Given a non-negative integer numRows, generate the first numRows of Pascal’s triangle.
Given a non-negative integer numRows, generate the first numRows of Pascal’s triangle.
Example:
Input: 5
Output:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
Solution
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> result(numRows);
for(int i = 0; i < numRows; ++i) {
result.at(i).resize(i + 1, 1);
if(i > 1) {
for(int j = 1; j < i; j++) {
result[i][j] = result[i - 1][j -1] + result[i - 1][j];
}
}
}
return result;
}
};
帕斯卡三角生成算法

本文介绍了一种生成帕斯卡三角的C++算法实现。输入一个非负整数numRows,该算法将生成帕斯卡三角的前numRows行。通过使用二维向量结构,算法有效地构建了三角形,并展示了其核心逻辑。
756

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



