-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascalTriangle.java
More file actions
27 lines (25 loc) · 826 Bytes
/
Copy pathPascalTriangle.java
File metadata and controls
27 lines (25 loc) · 826 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
import java.util.ArrayList;
import java.util.List;
public class PascalTriangle {
public static void main(String[] args) {
pascalTriangle(10);
}
private static List<List<Integer>> pascalTriangle(int count) {
List<List<Integer>> matrix = new ArrayList<>();
for (int i = 0; i < count; i++) {
List<Integer> arr = new ArrayList<>();
for (int j = 0; j <= i; j++) {
if (j == 0 || j == i) arr.add(1);
else arr.add(matrix.get(i - 1).get(j - 1) + matrix.get(i - 1).get(j));
}
matrix.add(arr);
}
for (List<Integer> list : matrix) {
for (Integer i : list) {
System.out.print(i + " ");
}
System.out.print("\n");
}
return matrix;
}
}