Given a non-negative integer numRows, generate the first numRows of Pascal’s triangle.
Given a non-negative integer numRows, generate the first numRows of Pascal’s triangle.
Example:
Input: 5
Output:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
Solution
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> result(numRows);
for(int i = 0; i < numRows; ++i) {
result.at(i).resize(i + 1, 1);
if(i > 1) {
for(int j = 1; j < i; j++) {
result[i][j] = result[i - 1][j -1] + result[i - 1][j];
}
}
}
return result;
}
};