1.题目:
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。
1
1 1
1 2 1
1 3 3 1
输入: 5
输出:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
2.代码:
/**
* Return an array of arrays.
* The sizes of the arrays are returned as *columnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int** generate(int numRows, int** columnSizes) {
int** r=(int **)malloc(sizeof(int *)*numRows);
*columnSizes=(int *)malloc(sizeof(int )*numRows);
for(int i=0;i<numRows;i++){
int *s=(int *)malloc(sizeof(int )*(i+1));
(*columnSizes)[i]=i+1;
s[0]=1;
s[i]=1;
if(i!=0)
for(int j=1;j<i;j++)
s[j]=r[i-1][j-1]+r[i-1][j];
r[i]=s;
}
return r;
}
3.知识点:
NULL;