[C语言] leetcode119. Pascal's Triangle II

    xiaoxiao2021-12-10  29

    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); }

    转载请注明原文地址: https://ju.6miu.com/read-700324.html

    最新回复(0)