-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascal.java
More file actions
39 lines (30 loc) · 970 Bytes
/
Pascal.java
File metadata and controls
39 lines (30 loc) · 970 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
36
37
38
39
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Pascal {
public static void main(String[] args){
int numRows = 14;
int rowIndex = 0;
List<Integer> lastList = new ArrayList<>();
lastList.add(1);
List<List<Integer>> pascal = new ArrayList<>();
for(int i = 1; i <= numRows; i++){
pascal.add(lastList);
lastList = makeNextList(lastList);
}
for(List<Integer> l: pascal){
System.err.println(l);
}
}
static List<Integer> makeNextList(List<Integer> list){
List<Integer> nextList = new ArrayList<>(list.size()+1);
for(int i = 0; i < list.size()+1; i++){
try {
nextList.add(i, list.get(i - 1) + list.get(i));
} catch (IndexOutOfBoundsException e) {
nextList.add(i, 1);
}
}
return nextList;
}
}