Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
In Pascal's triangle, each number is the sum of the two numbers directly above it.
Example:
Input: 5 Output: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> generate(int numRows) {
vector<vector<int>> ans;
for(int i = 0; i < numRows; ++i){
vector<int> temp;
temp.push_back(1);//首位
if(i == 0){
ans.push_back(temp);
continue;
}
vector<int> preRow(ans[i - 1]);
for(int j = 0; j < i - 1; ++j){
temp.push_back(preRow[j] + preRow[j + 1]);
}
temp.push_back(1);//末位
ans.push_back(temp);
}
return ans;
}