- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascalTri.java
More file actions
Latest commit
22 lines (22 loc) · 714 Bytes
/
Copy pathPascalTri.java
File metadata and controls
22 lines (22 loc) · 714 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
importjava.util.ArrayList;
importjava.util.*;
publicclassPascalTri {
publicList<List<Integer>> generate(intnumRows) {
List<List<Integer>> result = newArrayList<>();
// compute the first numRows of Pascal's triangle
for(inti = 0; i < numRows; i++){
List<Integer> innerList = newArrayList<>(i+1);
result.add(innerList);
innerList.add(0, 1);
if(i>0) innerList.add(1, 1);
intj = 1;
if(i>1){
while(j < i){
innerList.add(j, result.get(i-1).get(j-1) + result.get(i-1).get(j));
j++;
}
}
}
returnresult;
}
}