-
Notifications
You must be signed in to change notification settings - Fork 0
/
118.pascals-triangle.c
56 lines (50 loc) · 1.34 KB
/
118.pascals-triangle.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
/*
* @lc app=leetcode id=118 lang=c
*
* [118] Pascal's Triangle
*/
// @lc code=start
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
#include <stdlib.h>
int **generate(int numRows, int *returnSize, int **returnColumnSizes)
{
int **ret = (int **)malloc(sizeof(int *) * numRows);
*returnColumnSizes = (int *)malloc(sizeof(int) * numRows);
*returnSize = 0;
// init 1
*returnSize = 1;
(*returnColumnSizes)[0] = 1;
ret[0] = (int *)malloc(sizeof(int));
ret[0][0] = 1;
if (numRows == 1)
{
return ret;
}
for (int row = 2; row <= numRows; row++)
{
(*returnSize)++;
(*returnColumnSizes)[row - 1] = row;
ret[row - 1] = (int *)malloc(sizeof(int) * row);
for (int idx = 0; idx < row; idx++)
{
if (idx == 0)
{
ret[row - 1][idx] = ret[row - 2][idx];
}
else if (idx == row - 1)
{
ret[row - 1][idx] = ret[row - 2][idx - 1];
}
else
{
ret[row - 1][idx] = ret[row - 2][idx - 1] + ret[row - 2][idx];
}
}
}
return ret;
}
// @lc code=end