-
Notifications
You must be signed in to change notification settings - Fork 0
/
pascals-triangle.java
47 lines (37 loc) · 1.44 KB
/
pascals-triangle.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
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
if(numRows == 0) return result;
List<Integer> specList = new ArrayList<Integer>();
specList.add(1);
result.add(specList);
List<Integer> sList = new ArrayList<Integer>();
sList.add(1);
sList.add(1);
if(numRows == 1) return result;
if (numRows == 2){
result.add(sList);
return result;}
result.add(sList);
List<Integer> assignList = new ArrayList<Integer>();
for(int i=3;i<=numRows;i++){
List<Integer> prevList = new ArrayList<Integer>();
prevList = new ArrayList<>(assignList);
if(i==3) prevList = new ArrayList<>(sList);
List<Integer> curList = new ArrayList<Integer>();
/* As lists work with index 0 */
System.out.println("Size: "+ prevList.size()+" i:"+i);
curList.add(1);
int q =0;
for(int j=2;j<i; j++){
int val = prevList.get(q) + prevList.get(q+1);
curList.add(val);
q++;
}
curList.add(1);
assignList = new ArrayList<>(curList);
result.add(curList);
}
return result;
}
}