-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path118-Pascal's-Triangle(cached).cpp
49 lines (43 loc) · 1.19 KB
/
118-Pascal's-Triangle(cached).cpp
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
vector<vector<int>> cache;
int size = 0;
class Solution {
public:
vector<vector<int>> generate(int numRows) {
int line = 0;
if (numRows == 0) {
vector<vector<int>> result;
return result;
}
if (numRows > size) {
line = size;
size = numRows;
} else {
vector<vector<int>> result(&cache[0], &cache[numRows - 1]);
return result;
}
if (numRows >= 1 && line < 1) {
line++;
vector<int> row;
row.push_back(1);
cache.push_back(row);
}
if (numRows >= 2 && line < 2) {
line++;
vector<int> row;
row.push_back(1);
row.push_back(1);
cache.push_back(row);
}
for (; line < numRows; line++) {
vector<int> row;
row.push_back(1);
int line_1 = line - 1;
for (int j = 0; j < line_1;) {
row.push_back(cache[line_1][j] + cache[line_1][++j]);
}
row.push_back(1);
cache.push_back(row);
}
return cache;
}
};