-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathPascalsTriangle118.java
48 lines (35 loc) · 995 Bytes
/
PascalsTriangle118.java
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
40
41
42
43
44
45
46
47
48
package easy;
import java.util.ArrayList;
import java.util.List;
public class PascalsTriangle118 {
public static List<List<Integer>> generate(int numRows) {
List<List<Integer>> finalList = new ArrayList<>();
for (int row = 1; row <= numRows; row++) {
ArrayList<Integer> currentRowPrint = new ArrayList<>();
if (row == 1) {
currentRowPrint.add(1);
finalList.add(currentRowPrint);
} else {
currentRowPrint.add(1);
// filling the middle elements in row
List<Integer> toLoop = finalList.get(row - 2);
int slow = 0, fast = 1, high = toLoop.size() - 1;
while (fast <= high) {
int addItem = toLoop.get(slow) + toLoop.get(fast);
slow++;
fast++;
currentRowPrint.add(addItem);
}
currentRowPrint.add(1);
finalList.add(currentRowPrint);
}
}
return finalList;
}
public static void main(String[] args) {
System.out.println(generate(5));
}
}
/* ouptut:
[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]
*/