-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path118-Pascal's-Triangle.cpp
More file actions
53 lines (46 loc) · 1.2 KB
/
118-Pascal's-Triangle.cpp
File metadata and controls
53 lines (46 loc) · 1.2 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
50
51
52
53
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> vec;
vector<int> temp;
for(int i = 0; i < numRows; i++){
temp.clear();
if (i == 0){
temp.push_back(1);
vec.push_back(temp);
}
else if(i == 1){
temp.push_back(1);
temp.push_back(1);
vec.push_back(temp);
}
else{
for(int j = 0; j <= i; j++){
if(j == 0 || j == i){
temp.push_back(1);
}
else{
temp.push_back(vec[i-1][j-1] + vec[i-1][j]);
}
}
vec.push_back(temp);
}
}
return vec;
}
};
/* 118. Pascal's-Triangle.cpp
//////////////////////////////////////////////////
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
Input: 5
Output:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
https://leetcode.com/problems/pascals-triangle/
//////////////////////////////////////////////////
*/