-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascalTriangle.java
More file actions
28 lines (24 loc) · 825 Bytes
/
Copy pathPascalTriangle.java
File metadata and controls
28 lines (24 loc) · 825 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
public class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
if(numRows == 0) return result;
List<Integer> pre;
//The decleration inside the for loop is just valid between {};
//So there can be allocated multiple times, because it has been
//deallocated.
for (int i=0; i<numRows; i++){
List<Integer> row = new ArrayList<Integer>();
row.add(1);
for(int k=1; k<i; k++ ){
pre = result.get(i-1);
int temp = pre.get(k-1) + pre.get(k);
row.add(temp);
}
if(i!=0){
row.add(1);
}
result.add(row);
}
return result;
}
}