leetcode118-杨辉三角

 1 /**
 2  * Return an array of arrays.
 3  * The sizes of the arrays are returned as *columnSizes array.
 4  * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 5  */
 6 int** generate(int numRows, int** columnSizes) {
 7     
 8     int **ret;
 9     ret = (int **)malloc(sizeof(int *) * numRows);
10     *columnSizes =(int*) malloc(sizeof(int ) * numRows);
11     int i, j;
12     for(i = 0; i < numRows; i++)
13     {
14         (*columnSizes)[i] = i+1;
15         ret[i] = (int*)malloc(sizeof(int) *(i+1));
16         ret[i][0] = 1;
17         ret[i][i] = 1;
18     }
19 
20     for (i = 1; i < numRows; i++)
21     {
22          for (j = 1; j < i; j++)
23          {
24             ret[i][j] = ret[i-1][j-1] + ret[i-1][j];
25          }
26 
27     }
28     columnSizes = ret;
29     return columnSizes; 
30 }

猜你喜欢

转载自www.cnblogs.com/xinfenglee/p/10047969.html