-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPascalsTriangle.java
More file actions
32 lines (26 loc) · 844 Bytes
/
PascalsTriangle.java
File metadata and controls
32 lines (26 loc) · 844 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
import java.util.ArrayList;
import java.util.List;
public class PascalsTriangle {
public static void main(String[] args) {
for(List<Integer> list: generate(5)) {
System.out.println(list);
}
}
public static List<List<Integer>> generate(int numRows) {
List<List<Integer>> result = new ArrayList<>();
for (int i = 0; i < numRows; i++) {
List<Integer> list = new ArrayList<>();
for (int j = 0; j < i + 1; j++) {
if (j == 0 || j == i) {
list.add(1);
} else {
int a = result.get(i - 1).get(j - 1);
int b = result.get(i - 1).get(j);
list.add(a + b);
}
}
result.add(list);
}
return result;
}
}