-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascalTriangle.java
More file actions
26 lines (22 loc) · 875 Bytes
/
PascalTriangle.java
File metadata and controls
26 lines (22 loc) · 875 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
import java.util.ArrayList;
import java.util.List;
public class PascalTriangle {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> triangle = new ArrayList<>();
for (int row = 0; row < numRows; row++) {
List<Integer> currentRow = new ArrayList<>();
for (int col = 0; col <= row; col++) {
// The first and last row elements are always 1
if (col == 0 || col == row) {
currentRow.add(1);
} else {
// Each triangle element is the sum of the two elements above it
int value = triangle.get(row - 1).get(col - 1) + triangle.get(row - 1).get(col);
currentRow.add(value);
}
}
triangle.add(currentRow);
}
return triangle;
}
}