题目:
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] ]
题意给出行数,输出对应的杨辉三角形。
按题意用动态数组处理,除了最后一个数,其他数都是上一行相邻两个数的和。
class Solution {
public:
vector<vector<int> > generate(int numRows) {
// Note: The Solution object is instantiated only once and is reused by each test case.
vector<vector<int> > result;
if(numRows<1)return result;
vector<int> t;
t.push_back(1);
result.push_back(t);
for(int i=1;i<numRows;++i)
{
t.clear();
t.push_back(1);
for(int j=1;j<i;++j)
{
t.push_back(result[i-1][j-1]+result[i-1][j]);
}
t.push_back(1);
result.push_back(t);
}
return result;
}
};
// http://blog.csdn.net/havenoide