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] ]
教帕斯卡三角,其实就是中国的杨辉三角。它的两条斜边都是由数字1组成的,而其余的数则是等于它肩上的两个数之和。根据这个性质,不难写出以下程序。
Accepted Solution:
class Solution {
public:
vector<vector<int> > generate(int numRows)
{
vector<vector<int>> res;
for(int i=1;i<=numRows;i++)
{
vector<int> temp(i,1);
res.push_back(temp);
}
for(int i=0;i<numRows;i++)
{
for(int j=1;j<res[i].size()-1;j++)
{
res[i][j]=res[i-1][j-1]+res[i-1][j];
}
}
return res;
}
};