-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0118. Pascal's Triangle.cpp
More file actions
46 lines (36 loc) · 1.01 KB
/
0118. Pascal's Triangle.cpp
File metadata and controls
46 lines (36 loc) · 1.01 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
38
39
40
41
42
43
44
45
46
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> pascal;
vector<int> aux;
if(numRows <= 0){
return pascal;
}
if(numRows >= 1){
aux = {1};
pascal.push_back(aux);
aux.clear();
}
if(numRows >= 2){
aux = {1, 1};
pascal.push_back(aux);
aux.clear();
}
int cant = 2;
for(int i = 2; i < numRows; i++){
aux.clear();
//first position
aux.push_back(1);
//loop
for(int j = 0; j < cant-1; j++){
aux.push_back(pascal[i-1][j] + pascal[i-1][j+1]);
}
//last position
aux.push_back(1);
//add to central vector
pascal.push_back(aux);
cant++;
}
return pascal;
}
};