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]]
/** * 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**,其中保存着Pascal's Triangle,而*columnSizes则保存着每一行有多少个元素。 */ int** generate(int numRows, int** columnSizes) { int **arrcolumn = (int*)malloc(numRows*sizeof(int*)); *columnSizes = (int*)malloc(numRows*sizeof(int)); if (numRows == 0)return 0; *(arrcolumn) = (int*)malloc(sizeof(int*)); *(*(arrcolumn + 0) + 0) = 1; **columnSizes = 1; for (int i = 1; i<numRows; i++) { *(arrcolumn + i) = (int*)malloc((i + 1)*sizeof(int*)); *(*(arrcolumn + i) + 0) = 1; *(*(arrcolumn + i) + i) = 1; *(*(columnSizes)+i) = i + 1; for (int j = 1; j<i; j++) { *(*(arrcolumn + i) + j) = *(*(arrcolumn + i - 1) + j - 1) + *(*(arrcolumn + i - 1) + j); } } return arrcolumn; }