-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathPascalsTriangle.java
More file actions
28 lines (24 loc) 路 794 Bytes
/
Copy pathPascalsTriangle.java
File metadata and controls
28 lines (24 loc) 路 794 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
import java.util.ArrayList;
import java.util.List;
public class PascalsTriangle {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> result = new ArrayList<>();
if (numRows == 0) {
return result;
}
List<Integer> firstRow = new ArrayList<>();
firstRow.add(1);
result.add(firstRow);
for (int row = 1 ; row < numRows ; row++) {
List<Integer> list = new ArrayList<>(row + 1);
List<Integer> previous = result.get(result.size() - 1);
list.add(1);
for (int j = 1 ; j < row ; j++) {
list.add(previous.get(j - 1) + previous.get(j));
}
list.add(1);
result.add(list);
}
return result;
}
}