题意:给出n,求出n层的杨辉三角。
题解:ans[i][j] = ans[i - 1][j - 1] + ans[i - 1][j](j != 0 && j != i).
class Solution {
public:
vector<vector<int> > generate(int numRows) {
vector<vector<int> >ans;
if(numRows < 1) return ans;
vector<int> inner;
for(int i = 0; i < numRows; i++)
{
for(int j = 0; j <= i; j++)
{
if(j == 0 || j == i)
inner.push_back(1);
else
inner.push_back(ans[i - 1][j - 1] + ans[i - 1][j]);
}
ans.push_back(inner);
inner.clear();
}
return ans;
}
};