forked from aky91/Orange-Bloom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascal Triangle.java
More file actions
41 lines (26 loc) · 1.02 KB
/
Pascal Triangle.java
File metadata and controls
41 lines (26 loc) · 1.02 KB
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
//https://www.interviewbit.com/problems/pascal-triangle/
public class Solution {
public ArrayList<ArrayList<Integer>> generate(int A) {
ArrayList<ArrayList<Integer>> arr = new ArrayList<>();
if(A == 0)
return arr;
//insert 1st row
arr.add(new ArrayList<>());
arr.get(0).add(1);
for(int i = 1; i < A; i++){
arr.add(new ArrayList<>());
int size = arr.get(i - 1).size();
for(int j = 0; j < size + 1; j++){
int a = 0;
if(j - 1 >= 0 && j - 1 < size)
a = arr.get(i - 1).get(j - 1);
int b = 0;
if(j >= 0 && j < size)
b = arr.get(i - 1).get(j);
int ans = a + b;
arr.get(i).add(ans);
}
}
return arr;
}
}