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] ]
Subscribe to see which companies asked this questio
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> res;
vector<int> vec;
if(numRows <= 0) return res;
for(int i=1; i <= numRows; i++) {
vec.push_back(1);
for(int j = 1; i>2 && j < res[i-1-1].size(); j++) {
vec.push_back(res[i-2][j]+res[i-2][j-1]);
}
if(i > 1)
vec.push_back(1);
res.push_back(vec);
vec.clear();
}
return res;
}
}