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] ]
这道题是生成杨辉三角的前n行数据,题目难度为Easy。
杨辉三角相信大家都不陌生,每行首尾数字为1,其他部分数字为它两肩上两个数字之和,具体代码:
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> ret(numRows);
for(int i=0; i<numRows; ++i) {
ret[i].push_back(1);
for(int j=1; j<i; ++j)
ret[i].push_back(ret[i-1][j-1] + ret[i-1][j]);
if(i) ret[i].push_back(1);
}
return ret;
}
};