118. Pascal's Triangle
problem description
Input: 5
Output:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]algorithm thought
code
algorithm analysis
Last updated
Input: 5
Output:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]Last updated
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> res;
if(numRows==0)
return res;
vector<int> tmp(1,1);
res.push_back(tmp);
while(--numRows){
tmp.push_back(1);
for(int i=tmp.size()-2;i>=1;--i){
tmp[i]+=tmp[i-1];
}
res.push_back(tmp);
}
return res;
}
};