杨辉三角
程序员文章站
2022-04-22 10:57:51
...
Given a non-negative integer numRows, generate the first numRows of Pascal’s triangle.
class Solution {
public:
vector<vector<int>> generate(int n) {
vector<vector<int>> res;
vector<int> v = {1};
if (n==0) return res;
res.push_back(v);
for (int i = 1; i < n; ++i) {
vector<int> v = {1};
for (int j = 1; j < i; ++j) {
int x = res[i-1][j-1]+res[i-1][j];
v.push_back(x);
}
v.push_back(1);
res.push_back(v);
}
return res;
}
};
上一篇: 杨辉三角
下一篇: 电话薄程序java程序实现