From : https://leetcode.com/problems/pascals-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>> res;
if(numRows <= 0) return res;
int i=1;
vector<int> pre;
pre.push_back(1);
res.push_back(pre);
while(i < numRows) {
pre = res[i-1];
vector<int> cur;
for(int j=0; j<=i; j++) {
if(j==0 || j==i) cur.push_back(1);
else {
cur.push_back(pre[j-1] + pre[j]);
}
}
res.push_back(cur);
i++;
}
return res;
}
};
1037

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



