- C语言
- 注意点
- 结果
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* getRow(int rowIndex, int* returnSize){
int i,j;
int len;
int rlen;
int **dp = NULL;
len = rowIndex + 1;
dp = calloc(rowIndex+1, sizeof(int *));
dp[0] = calloc(1, sizeof(int));
dp[0][0] = 1;
for (i = 1; i < rowIndex + 1; i++) {
rlen = i + 1;
dp[i] = calloc(len, sizeof(int));
dp[i][0] = dp[i-1][0];
dp[i][rlen-1] = dp[i-1][i-1];
for(j = 1; j < rlen - 1; j++) {
dp[i][j] = dp[i-1][j-1] + dp[i-1][j];
}
}
*returnSize = len;
//实战编程中注意释放
return dp[rowIndex];
}
注意点
- 注意每行长度的规律是
rowIndex+1
; - 每行的头
[0]
和尾[rlen-1]
都是相等的,此处是1; - 内存注意优化;
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)