-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
34 lines (26 loc) · 925 Bytes
/
solution.java
File metadata and controls
34 lines (26 loc) · 925 Bytes
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
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> result = new ArrayList<>();
//Row: 1
result.add(List.of(1));
if(numRows == 1)
return result;
//Row: 2
result.add(List.of(1, 1));
if(numRows == 2)
return result;
//Row: 2+
for(int r = 2; r < numRows; r++){
List<Integer> prev = result.get(r - 1);
List<Integer> next = new ArrayList<>();
next.add(prev.get(0)); //First Entry
//Middle calculations
for(int n = 0; n < prev.size() - 1; n++){
next.add(prev.get(n) + prev.get(n + 1));
}
next.add(prev.get(prev.size() - 1)); //Last Entry
result.add(next);
}
return result;
}
}