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] ]
» Solve this problem
class Solution {
public:
vector<vector<int> > generate(int numRows) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int> > answer;
if (numRows <= 0) {
return answer;
}
vector<int> temp;
for (int i = 1; i <= numRows; i++) {
temp.clear();
temp.push_back(1);
for (int j = 2; j <= i - 1; j++) {
temp.push_back(answer[i - 2][j - 2] + answer[i - 2][j - 1]);
}
if (i != 1) {
temp.push_back(1);
}
answer.push_back(temp);
}
return answer;
}
};