Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> v ;
for(int i = 0; i < numRows; ++i){
v.push_back(vector<int>(i+1,1)); //!!!!!!!!!!!!!!!!!!!!!!
for(int j = 1; j < i; ++j){
v[i][j] = v[i-1][j-1] + v[i-1][j];
}
}
return v;
}
};
本文介绍了一种生成帕斯卡三角形的算法实现。通过使用C++代码,该算法可以生成任意层数的帕斯卡三角形。具体实现中,首先创建了一个二维向量来存放每一层的数据,并为每层的首尾元素赋值为1,然后利用前一层的数据填充当前层的中间元素。
1013

被折叠的 条评论
为什么被折叠?



