leetcode链接:https://leetcode.cn/problems/pascals-triangle/description/
代码:
class Solution {
public:
vector<vector<int>> generate(int numRows) {
// 先开空间
vector<vector<int>> v;
v.resize(numRows);
for (int i = 0; i < v.size(); i++) {
v[i].resize(i + 1);
// 第一个元素和最后一个元素初始化为1
v[i][0] = 1;
v[i][v[i].size() - 1] = 1;
}
for (int i = 2; i < v.size(); i++) {
for (int j = 1; j < i; j++) {
v[i][j] = v[i - 1][j] + v[i - 1][j - 1];
}
}
return v;
}
};