Skip to content

Commit 2abc298

Browse files
committed
"Pascal's Triangle"
1 parent 3d1a37b commit 2abc298

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ them with C++ Language.
7676
| 121 | [Best Time to Buy and Sell Stock] | [C](src/121.c) |
7777
| 120 | [Triangle] | [C](src/120.c) |
7878
| 119 | [Pascal's Triangle II] | |
79-
| 118 | [Pascal's Triangle] | |
79+
| 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] | |
8282
| 115 | [Distinct Subsequences] | |

src/118.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
class Solution {
7+
public:
8+
vector<vector<int> > generate(int numRows) {
9+
vector<vector<int> > ans;
10+
11+
for (int i = 0; i < numRows; i++) {
12+
ans.push_back(vector<int>(i + 1, 0));
13+
ans[i][0] = 1;
14+
for (int j = 1; j < i; j++) {
15+
ans[i][j] = ans[i - 1][j - 1] + ans[i - 1][j];
16+
}
17+
ans[i][i] = 1;
18+
}
19+
return ans;
20+
}
21+
};
22+
23+
int main() {
24+
Solution s;
25+
vector<vector<int> > ret;
26+
ret = s.generate(5);
27+
28+
for (unsigned i = 0; i < ret.size(); i++) {
29+
for (unsigned j = 0; j < i + 1; j++) {
30+
cout << ret[i][j] << ' ';
31+
}
32+
cout << endl;
33+
}
34+
return 0;
35+
}

0 commit comments

Comments
 (0)