题目描述
在杨辉三角中,每个数是它左上方和右上方的数的和。
输入: 5
输出:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
代码
int** generate(int numRows, int* returnSize, int** returnColumnSizes){
int row = 0;
int col = 0;
int** ppRes = (int**)malloc(numRows * sizeof(int*));
for (row = 0; row <= numRows - 1; row++)
{
ppRes[row] = (int*)malloc(numRows * sizeof(int));
}
*returnSize = numRows;
*returnColumnSizes = (int*)malloc(numRows * sizeof(int));
for (row = 0; row <= numRows - 1; row++)
{
for (col = 0; col <= row; col++)
{
if (0 == col || row == col)
{
ppRes[row][col] = 1;
}
else
{
ppRes[row][col] = ppRes[row - 1][col - 1] + ppRes[row - 1][col];
}
}
(*returnColumnSizes)[row] = row + 1;
}
return ppRes;
}