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> > ans;
if(numRows <= 0)
return ans;
vector<int> v;
v.push_back(1);
ans.push_back(v);
for(int i=1; i<numRows; i++)
{
vector<int> v1;
v1.push_back(1);
for(int j=1; j<i; j++)
{
v1.push_back(ans[i-1][j-1] + ans[i-1][j]);
}
v1.push_back(1);
ans.push_back(v1);
}
return ans;
}
};
本文介绍了一种生成帕斯卡三角形的算法实现。通过C++代码详细展示了如何根据输入的行数生成对应的帕斯卡三角形,并提供了完整的代码示例。
482

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



