-
Notifications
You must be signed in to change notification settings - Fork 118
/
118. Pascal's Triangle.cpp
48 lines (40 loc) · 1.01 KB
/
118. Pascal's Triangle.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
/**
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
In Pascal's triangle, each number is the sum of the two numbers directly above it.
Example:
Input: 5
Output:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
**/
/**
Approach 1: Dynamic Programming
**/
/**
Complexity Analysis
Time complexity : O(numRows^2)
Space complexity : O(numRows^2)
**/
//Runtime: 8 ms, faster than 25.38% of C++ online submissions for Pascal's Triangle.
//Memory Usage: 8.7 MB, less than 86.38% of C++ online submissions for Pascal's Triangle.
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> ans;
for(int i = 1; i <= numRows; i++){
vector<int> v = vector<int>(i, 1);
if(i > 2){
for(int j = 1; j <= v.size()-2; j++){
v[j] = ans[ans.size()-1][j-1] + ans[ans.size()-1][j];
}
}
ans.push_back(v);
}
return ans;
}
};