-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
35 lines (27 loc) · 845 Bytes
/
Copy pathSolution.java
File metadata and controls
35 lines (27 loc) · 845 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
35
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> result = new ArrayList<>(numRows);
generate(numRows, result);
return result;
}
private void generate(int numRows, List<List<Integer>> result) {
if (numRows == 1) {
result.add(List.of(1));
return;
}
if (numRows == 2) {
result.add(List.of(1));
result.add(List.of(1, 1));
return;
}
generate(numRows - 1, result);
List<Integer> previous = result.get(result.size() - 1);
List<Integer> res = new ArrayList<>();
res.add(1);
for (int i = 1; i < previous.size(); i++) {
res.add(previous.get(i) + previous.get(i-1));
}
res.add(1);
result.add(res);
}
}