[LeetCode]Pascal's Triangle
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 {
private:
vector<vector<int> > res;
vector<int> ans;
public:
vector<vector<int> > generate(int numRows) {
if(numRows==0) return res;
int row=0;
while(row<numRows)
{
ans.clear();
for(int i=0;i<=row;i++)
{
if(i==0||i==row) ans.push_back(1);
else ans.push_back(res[row-1][i-1]+res[row-1][i]);
}
row++;
res.push_back(ans);
}
return res;
}
};

浙公网安备 33010602011771号