We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 98b6666 commit 21027b8Copy full SHA for 21027b8
c/118-Pascals-triangle.c
@@ -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