-
Notifications
You must be signed in to change notification settings - Fork 0
/
119 Pascal's Triangle II.c
60 lines (53 loc) · 1.59 KB
/
119 Pascal's Triangle II.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* getRow(int rowIndex, int* returnSize)
{
int * ret_arr = (int *)malloc(sizeof(int) * (rowIndex+1));
int ret_arr_index = 0;
int ** tmp_ptr = (int **)malloc(sizeof(int *) * (rowIndex+1));
int tmp_ptr_index = 0;
int last_row_index = 0;
int i=0;
for(i=0; i<=rowIndex; i++)
{
if(i==0)
{
int * new_arr = (int *)malloc(sizeof(int) *(i+1));
tmp_ptr[tmp_ptr_index++] = new_arr;
new_arr[0] = 1;
}
else if(i == 1)
{
int k=0;
int * new_arr = (int *)malloc(sizeof(int) * (i+1));
int new_arr_index = 0;
tmp_ptr[tmp_ptr_index++] = new_arr;
for(k=0; k<=i; k++)
{
new_arr[new_arr_index++] = 1;
}
}
else
{
int k=0;
int * new_arr = (int *)malloc(sizeof(int) * (i+1));
int new_arr_index = 0;
tmp_ptr[tmp_ptr_index++] = new_arr;
for(k=0; k<=i; k++)
{
if(k==0 || k==i)
{
new_arr[new_arr_index++] = 1;
}
else
{
new_arr[new_arr_index++] = tmp_ptr[tmp_ptr_index-2][k-1] + tmp_ptr[tmp_ptr_index-2][k];
}
}
}
}
memcpy(ret_arr, &tmp_ptr[tmp_ptr_index-1][0], sizeof(int) * tmp_ptr_index);
*returnSize = tmp_ptr_index;
return ret_arr;
}