-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathPascalsTriangleII.java
More file actions
35 lines (29 loc) · 876 Bytes
/
PascalsTriangleII.java
File metadata and controls
35 lines (29 loc) · 876 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
28
29
30
31
32
33
34
35
class PascalsTriangleII {
public List<Integer> getRow(int rowIndex) {
List<Integer> newList = new ArrayList<>();
List<Integer> previousList = new ArrayList<>();
if(rowIndex==0){
newList.add(1);
return newList;
}
previousList.add(1);
previousList.add(1);
if(rowIndex==1){
return previousList;
}
int count =1;
while(count<rowIndex) {
newList = new ArrayList<>();
for(int i=0;i<previousList.size()+1;i++){
if((i==0) || i==previousList.size()){
newList.add(1);
} else {
newList.add(previousList.get(i) + previousList.get(i-1));
}
}
previousList = newList;
count++;
}
return newList;
}
}