[LeetCode118]Pascal's Triangle

时间:2023-03-08 17:39:52
[LeetCode118]Pascal's Triangle

题目:

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>> vec1;
for(int i = ; i < numRows; ++i)
{
vector<int> vec2;
vec2.push_back();
for(int j = ; j < i; ++j)
{
int tmp = vec1[i-][j-] + vec1[i-][j];
vec2.push_back(tmp);
}
if(i != )
vec2.push_back();
vec1.push_back(vec2);
}
return vec1;
}
};