https://leetcode-cn.com/problems/pascals-triangle/
题目
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。
在杨辉三角中,每个数是它左上方和右上方的数的和。
输入: 5
输出:
[
[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;
for (int i = 0; i < numRows; i++) {
vector <int> temp;
for (int j = 0; j <= i; j++) {
if (!j || j == i) temp.push_back(1);
else temp.push_back(ans[i - 1][j] + ans[i - 1][j - 1]);
}
ans.push_back(temp);
}
return ans;
}
};