1.题目要求如图所示:
示例 1:输入: rowIndex = 3
输出: [1,3,3,1]
示例 2:输入: rowIndex = 0
输出: [1]
示例 3:输入: rowIndex = 1
输出: [1,1]
先用malloc函数创造一个二维数组,变成杨辉三角,然后再用一维数组找到所指的那一行:
/*** Note: The returned array must be malloced, assume caller calls free().*/
int* getRow(int rowIndex, int* returnSize) {int order = rowIndex + 1;int** triangle = (int**)malloc(sizeof(int*) * order);int i = 0;int l = 1;for(i = 0;i < order;i++){triangle[i] = (int*)malloc(sizeof(int) * l);l++;}int j = 0;for(i = 0;i < order;i++){for(j = 0;j <= i;j++){if(j == 0){triangle[i][j] = 1;}else if(j == i){triangle[i][j] = 1;}else{triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j];}}}int* number = (int*)malloc(sizeof(int) * order);*returnSize = order;int f = 0;j = 0;for(j = 0;j < order;j++){number[f] = triangle[rowIndex][j];f++;}return number;
}