天天看点

[C语言] leetcode 119. Pascal's Triangle II

Given an index k, return the kth row of the Pascal's triangle.

For example, given k = 3,

Return 

[1,3,3,1]

.

/**
 * Return an array of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* getRow(int rowIndex, int* returnSize) {
    int i,j;
    int** returnarray=(int**)malloc((rowIndex+1)*sizeof(int*));
    for(i=0;i<=rowIndex;i++)
    {
        *(returnarray+i)=(int*)malloc((i+1)*sizeof(int));
        for(j=0;j<=i;j++)
        {
            if(j==0|j==i) 
            {
                *(*(returnarray+i)+j)=1;
            }
            else
            {
                *(*(returnarray+i)+j)=*(*(returnarray+i-1)+j-1)+*(*(returnarray+i-1)+j);
            }
        }
    }
    *returnSize=rowIndex+1;
    return *(returnarray+rowIndex);
}           

继续阅读