Skip to content

Commit 21027b8

Browse files
Add 118 in c language
1 parent 98b6666 commit 21027b8

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

c/118-Pascals-triangle.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Given an integer numRows, return the first numRows of Pascal's triangle.
3+
4+
Space: O(n²) (n=numRows)
5+
Time: O(n²)
6+
*/
7+
8+
int** generate(int numRows, int* returnSize, int** returnColumnSizes){
9+
*returnSize = numRows;
10+
(*returnColumnSizes) = malloc(sizeof(int*)*numRows);
11+
int** ans = malloc(sizeof(int*)*numRows);
12+
for (int i=0; i<numRows; i++) {
13+
(*returnColumnSizes)[i] = i+1;
14+
ans[i] = malloc(sizeof(int)*(i+1));
15+
ans[i][0] = 1;
16+
ans[i][i] = 1;
17+
}
18+
for (int i=2; i<numRows; i++) {
19+
for (int j=1; j<i; j++) {
20+
ans[i][j] = ans[i-1][j-1] + ans[i-1][j];
21+
}
22+
}
23+
return ans;
24+
}

0 commit comments

Comments
 (0)