Skip to content

Commit 617e0e3

Browse files
authored
Create 118. Pascal's Triangle
1 parent 9fd9092 commit 617e0e3

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

118. Pascal's Triangle

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> generate(int numRows) {
4+
if (numRows == 0) return {};
5+
if (numRows == 1) return {{1}};
6+
7+
vector<vector<int>> prevRows = generate(numRows - 1);
8+
vector<int> newRow(numRows, 1);
9+
10+
for (int i = 1; i < numRows - 1; i++) {
11+
newRow[i] = prevRows.back()[i - 1] + prevRows.back()[i];
12+
}
13+
14+
prevRows.push_back(newRow);
15+
return prevRows;
16+
}
17+
};

0 commit comments

Comments
 (0)