-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascal_Triangle_2.cpp
More file actions
49 lines (41 loc) · 1.23 KB
/
Copy pathPascal_Triangle_2.cpp
File metadata and controls
49 lines (41 loc) · 1.23 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
47
48
49
/* recursive solution */
class Solution {
public:
vector<int> getRow(int rowIndex) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(rowIndex == 0) return vector<int>(1, 1);
vector<int> p_row = getRow(rowIndex-1);
vector<int> c_row;
c_row.push_back(1);
for(int i = 1; i < p_row.size(); i++){
c_row.push_back(p_row[i] + p_row[i-1]);
}
c_row.push_back(1);
return c_row;
}
};
/* iterative solution */
class Solution {
public:
vector<int> getRow(int rowIndex) {
if(rowIndex < 0) return vector<int>();
vector<int> layer = {1};
vector<int> next_layer;
int k = 1;
int t;
while(k <= rowIndex){
for(int i = 0; i <= k; i++){
if(i > 0) t = layer[i-1];
else t = 0;
if(i < k) t += layer[i];
else t += 0;
next_layer.push_back(t);
}
layer = next_layer;
next_layer.clear();
k++;
}
return layer;
}
};