-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
31 lines (24 loc) · 843 Bytes
/
Solution.java
File metadata and controls
31 lines (24 loc) · 843 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
public class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
if (numRows < 1)
return result;
List<Integer> firstElem = new ArrayList<Integer>();
firstElem.add(1);
result.add(firstElem);
if (numRows == 1) {
return result;
}
for (int i = 2; i <= numRows; i++) {
List<Integer> row = new ArrayList<Integer>();
row.add(1);
List<Integer> preRow = result.get(i - 2);
for (int j = 1; j < i - 1; j++) {
row.add(preRow.get(j - 1) + preRow.get(j));
}
row.add(1);
result.add(row);
}
return result;
}
}