-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path118_Pascal's Triangle.cpp
More file actions
37 lines (34 loc) · 1.16 KB
/
118_Pascal's Triangle.cpp
File metadata and controls
37 lines (34 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> lines;
vector<int> oneLine;
if (numRows <= 0){
lines.clear();
}else if (numRows == 1){
oneLine.clear(); oneLine.push_back(1); lines.push_back(oneLine);
}else if (numRows == 2){
oneLine.clear(); oneLine.push_back(1); lines.push_back(oneLine);
oneLine.clear(); oneLine.push_back(1); oneLine.push_back(1); lines.push_back(oneLine);
}else {
oneLine.clear(); oneLine.push_back(1); lines.push_back(oneLine);
oneLine.clear(); oneLine.push_back(1); oneLine.push_back(1); lines.push_back(oneLine);
for (int i = 2; i < numRows; i++){
oneLine.clear(); oneLine.push_back(1);
for (int j = 1; j <= i -1; j++){
oneLine.push_back(lines[i-1][j-1] + lines[i-1][j]);
}
oneLine.push_back(1); lines.push_back(oneLine);
}
}
return lines;
}
};
int main() {
Solution s;
s.generate(5);
return 0;
}