-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0118. Pascal's Triangle.java
More file actions
49 lines (39 loc) · 1.11 KB
/
0118. Pascal's Triangle.java
File metadata and controls
49 lines (39 loc) · 1.11 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
/*
Idea: Rather than looking the triangle as a growing tree from the middle
it can be looked something like this.
Ex: numRows = 5
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
using curr & prev:
what we do here is that everytime we add 0 to curr's position 0 and add all the previous numbers
according to the indexes
Format used: curr + prev
Ex: numRows = 5
[0] + [1]
[0, 1] + [1]
[0, 1, 2, 1] + [1, 1]
[0, 1, 3, 3, 1] + [1, 2, 1]
[1, 4, 6, 4, 1] (we end up with this result)
*/
class Solution {
public List<List<Integer>> generate(int numRows) {
Stack<List<Integer>> res = new Stack<>();
List<Integer> prev = new ArrayList<>();
prev.add(1);
res.add(prev);
while(res.size() < numRows){
prev = res.peek();
List<Integer> curr = new ArrayList<>();
curr.add(0);
curr.addAll(prev);
for(int i = 0; i < prev.size(); ++i){
curr.set(i, prev.get(i) + curr.get(i));
}
res.add(curr);
}
return res;
}
}