LeetCode119杨辉三角2

给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 行。

在杨辉三角中,每个数是它左上方和右上方的数的和。

示例:

输入: 3
输出: [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** array = (int **)malloc((rowIndex + 1) * sizeof(int *));
    *returnSize = rowIndex + 1;
    for(i = 0; i < rowIndex + 1; i++){
        array[i] = (int *)malloc((i + 1) * sizeof(int));
        for(j = 0; j < i + 1; j++){
            if((j == 0) || (j == i))
                array[i][j] = 1;
            else
                array[i][j] = array[i - 1][j - 1] + array[i - 1][j];
        }
    }
    return array[rowIndex];
}

猜你喜欢

转载自blog.csdn.net/a_learning_boy/article/details/84545510