形如:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]的三角形称为杨辉三角行。
给定函数n,构建一个n行的杨辉三角
vector<vector<int>> triangle(int n)
{
vector<vector<int>> res;
if(n<=0 )
return res;
for(int row = 0; row<n; ++row)
{
vector<int> temp(row+1, 1);
for(int col = 1; col<row; ++col)
temp[col] = res[row-1][col-1] + res[row-1][col];
res[row] = temp;
}
return res;
}
网友评论