Skip to content

Commit 5a7e710

Browse files
committed
"Pascal's Triangle II"
1 parent 2abc298 commit 5a7e710

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ them with C++ Language.
7575
| 122 | [Best Time to Buy and Sell Stock II] | [C](src/122.c) |
7676
| 121 | [Best Time to Buy and Sell Stock] | [C](src/121.c) |
7777
| 120 | [Triangle] | [C](src/120.c) |
78-
| 119 | [Pascal's Triangle II] | |
78+
| 119 | [Pascal's Triangle II] | [C](src/119.c) |
7979
| 118 | [Pascal's Triangle] | [C](src/118.cpp) |
8080
| 117 | [Populating Next Right Pointers in Each Node II] | |
8181
| 116 | [Populating Next Right Pointers in Each Node] | |
@@ -224,7 +224,7 @@ them with C++ Language.
224224
[Intersection of Two Linked Lists]: https://leetcode.com/problems/intersection-of-two-linked-lists/
225225
[Longest Substring with At Most Two Distinct Characters]: https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/
226226
[Read N Characters Given Read4 II - Call multiple times]: https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/
227-
[Read N Characters Given Read4]: https://leetcode.com/problems/read4-n-characters-given-read/
227+
[Read N Characters Given Read4]: https://leetcode.com/problems/read-n-characters-given-read4/
228228
[Binary Tree Upside Down]: https://leetcode.com/problems/binary-tree-upside-down/
229229
[Min Stack]: https://leetcode.com/problems/min-stack/
230230
[Find Minimum in Rotated Sorted Array II]: https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/

src/119.c

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
int *getRow(int rowIndex) {
5+
int k = rowIndex;
6+
int *ans = (int *)calloc(k + 1, sizeof(int));
7+
int i, j;
8+
int prev = 0, cur = 0;
9+
ans[0] = 1;
10+
for (i = 1; i < k + 1; i++) {
11+
prev = ans[0];
12+
for (j = 1; j < i; j++) {
13+
cur = ans[j];
14+
ans[j] += prev;
15+
prev = cur;
16+
}
17+
ans[i] = 1;
18+
}
19+
return ans;
20+
}
21+
22+
int main() {
23+
int k = 5;
24+
int *ans = getRow(k);
25+
int i;
26+
for (i = 0; i < k + 1; i++) {
27+
printf("%d ", ans[i]);
28+
}
29+
printf("\n");
30+
return 0;
31+
}

0 commit comments

Comments
 (0)