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],

]

/**
 * 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().
 */
/**
 * 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** generate(int numRows, int** columnSizes) 
{
    if(numRows < 1)
        return NULL;
    
    int i = 0;
    int j = 0;
    
    /*申请数组大小,数组元素为指针,即指针数组*/
    int **returnarry = (int **)malloc(numRows *sizeof(int *));
    *columnSizes =(int *)malloc(numRows * sizeof(int));
    for(i = 0;i < numRows;i++)
    {
        /*(*columnSize)才是malloc的内存地址,由于运算符优先级低于[],所以需要加括号*/
        (*columnSizes)[i] = i + 1;
        returnarry[i] = (int *)malloc(sizeof(int)*(i + 1));
        for(j = 0;j < i + 1;j++)
        {
            if( 0 == j  || i == j )
                returnarry[i][j] = 1;
            else
                returnarry[i][j] = returnarry[i-1][j-1] + returnarry[i-1][j];
        }
    }
    return returnarry;
    
}

经验教训

无法采用columnSizes的申请内存的方式,以*columnSizes = 这种形式,如果int **returnarry = NULL;
然后*returnarray = (int *)malloc(numRows * sizeof(int ));
这种形式就会出错。原因是*NULL肯定是错的。

我的理解是columnSizes由于是传进来的,*columnSizes不等于NULL,另外还有一点理解是比如我要带出去一个int型变量的值,要用一级指针,我要修改指针本身,就得用二级指针。