-
Notifications
You must be signed in to change notification settings - Fork 119
/
118.c
44 lines (36 loc) · 1.14 KB
/
118.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <stdlib.h>
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *columnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int** generate(int numRows, int** columnSizes, int* returnSize) {
*returnSize = numRows;
int **ans = (int **)calloc(*returnSize, sizeof(int *));
(*columnSizes) = (int *)calloc(*returnSize, sizeof(int));
int i, j;
for (i = 0; i < *returnSize; i++) {
(*columnSizes)[i] = i + 1;
ans[i] = (int *)calloc((*columnSizes)[i], sizeof(int));
ans[i][0] = 1;
for (j = 1; j < i; j++) {
ans[i][j] = ans[i - 1][j - 1] + ans[i - 1][j];
}
ans[i][i] = 1;
}
return ans;
}
int main() {
int returnSize = 0;
int *columnSizes = NULL;
int **ret = generate(5, &columnSizes, &returnSize);
int i, j;
for(i = 0; i < returnSize; i++) {
for (j = 0; j < columnSizes[i]; j++) {
printf("%d ", ret[i][j]);
}
printf("\n");
}
return 0;
}