class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> res;
vector<int> row;
if(numRows==0)
return res;
row.push_back(1);
res.push_back(row);
int i,j;
for(i=2;i<=numRows;i++)
{
row=res[res.size()-1];
vector<int> newRow;
newRow.push_back(row[0]);
for(j=1;j<row.size();j++)
{
newRow.push_back(row[j-1]+row[j]);
}
newRow.push_back(row[0]);
res.push_back(newRow);
}
return res;
}
};118. Pascal's Triangle
最新推荐文章于 2022-10-24 16:00:51 发布
本文介绍了一个C++类Solution,该类包含一个名为generate的方法,用于生成指定行数的杨辉三角。通过迭代的方式构建每行的数据,利用上一行的数据来计算当前行的元素。
1016

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



