leetcode Pascal's triangle

#include <stdio.h>
int** generate(int numRows, int** columnSizes) {
    if (numRows == 0) {
        columnSizes = NULL;
      return NULL;
    }
    int** res = NULL;
    res= (int **) malloc (numRows*sizeof(int *));
    (*columnSizes) = (int *) malloc(numRows*sizeof(int));
    int i,j;
    for (i = 0; i< numRows; i++) {
        res[i] = (int*) malloc((i+1)*sizeof(int));
        (*columnSizes)[i] = i+1;
        if (0==i) {
            res[i][0] = 1;
        }
        else  {
            res[i][0] = 1;
            res[i][i] = 1;
             if (i > 1) {
                 for (j =1; j<i;j++) {
                     res[i][j] = res[i-1][j-1]+ res[i-1][j];
                 }
             }
        }
    }
    return res;
}
int main (int argc, char * argv[]) {
    int *Size = NULL;
    int **res = generate(5, &Size);
    if (Size != NULL && res != NULL) {
        int i,j;
        for (i =0; i<5; i++) {
            for (j = 0; j< Size[i];j++) {
                fprintf(stdout,"%d 
",res[i][j]);
            }
            fprintf(stdout,"
");
        }
        for (i = 0; i < 5;i++) {
            if (res[i] != NULL) {
                free(res[i]);
                res[i]=NULL;
            }
        }
        free(res);
        res = NULL;
        if (Size != NULL) {
            free(Size);
            Size = NULL;
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/finallyliuyu/p/6003042.html