Skip to content

Commit

Permalink
Add 118 in c language
Browse files Browse the repository at this point in the history
  • Loading branch information
julienChemillier authored Oct 27, 2022
1 parent 98b6666 commit 21027b8
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions c/118-Pascals-triangle.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Given an integer numRows, return the first numRows of Pascal's triangle.
Space: O(n²) (n=numRows)
Time: O(n²)
*/

int** generate(int numRows, int* returnSize, int** returnColumnSizes){
*returnSize = numRows;
(*returnColumnSizes) = malloc(sizeof(int*)*numRows);
int** ans = malloc(sizeof(int*)*numRows);
for (int i=0; i<numRows; i++) {
(*returnColumnSizes)[i] = i+1;
ans[i] = malloc(sizeof(int)*(i+1));
ans[i][0] = 1;
ans[i][i] = 1;
}
for (int i=2; i<numRows; i++) {
for (int j=1; j<i; j++) {
ans[i][j] = ans[i-1][j-1] + ans[i-1][j];
}
}
return ans;
}

0 comments on commit 21027b8

Please sign in to comment.