leetcode[118]:Pascal's Triangle

Pascal’s Triangle

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 of size *returnSize.
 * 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* returnSize) {
    int **result;
    int i,j,k;
    *returnSize = numRows;
    if (numRows==0)  return NULL;
    result= (int**)malloc(sizeof(int*)*numRows);
    *columnSizes = (int*)malloc(sizeof(int*)*numRows);
    result[0] = (int*)malloc(sizeof(int*));
    result[0][0]=1;
    (*columnSizes)[0]=1;

    for(i=1;i<numRows;i++)
    {
         result[i] = (int*)malloc(sizeof(int*)*(i+1));
         result[i][0]= result[i-1][0];
         printf("%d ",result[i][0]);
         for(j=1;j<i;j++)
         {
             result[i][j]= result[i-1][j-1]+result[i-1][j];
             printf("%d ",result[i][j]);
         }
        result[i][j]= result[i-1][i-1];
        printf("%d\n",result[i][j]);
        (*columnSizes)[i]=i+1;
    }
    return result;
}
發佈了85 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章