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] ]1st iteration:
using dp, just like Pascal's Triangle II, but note there is minor difference, here the numRows is not 0-based but 1-based .
class Solution {
public:
vector<vector<int> > generate(int numRows) {
assert(numRows >= 0);
vector<vector<int> > result;
if (numRows == 0) {
return result;
}
vector<int> row(numRows+1,0);
row[1] = 1;
result.push_back(vector<int>(row.begin()+1, row.begin()+2));
for (int i = 2; i <= numRows; i++) {
for (int j = numRows; j > 0; j--) {
row[j] += row[j-1];
}
result.push_back(vector<int>(row.begin()+1, row.begin()+i+1));
}
return result;
}
};