给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。
在杨辉三角中,每个数是它左上方和右上方的数的和。
示例:
输入: 5
输出:
[
]
/** * Return an array of arrays of size *returnSize. * The sizes of the arrays are returned as *returnColumnSizes array. * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free(). */ int** generate(int numRows, int* returnSize, int** returnColumnSizes){ *returnSize=numRows; * returnColumnSizes=NULL; if(numRows<=0)return NULL; int **ret=(int **)malloc(sizeof(int *)); ret[0]=(int *)malloc(sizeof(int)); ret[0][0]=1; * returnColumnSizes=(int *)malloc(sizeof(int)); (* returnColumnSizes)[0]=1; for(int i=1;i<numRows;i++) { * returnColumnSizes=(int *)realloc(* returnColumnSizes,sizeof(int)*(i+1)); (* returnColumnSizes)[i]=i+1; ret=(int **)realloc(ret,sizeof(int *)*(* returnColumnSizes)[i]); ret[i]=(int*)malloc(sizeof(int)*(* returnColumnSizes)[i]); ret[i][0]=1; ret[i][i]=1; for(int j=1;j<i;j++) { ret[i][j]=ret[i-1][j-1]+ret[i-1][j]; } } return ret; }
执行用时 :0 ms, 在所有C提交中击败了100.00% 的用户
内存消耗 :7.2 MB, 在所有C提交中击败了5.69%的用户
文章来源: https://blog.csdn.net/xuyuanwang19931014/article/details/91489197