forked from hongtaocai/code_interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpascalstriangle.java
More file actions
executable file
·34 lines (31 loc) · 1.1 KB
/
pascalstriangle.java
File metadata and controls
executable file
·34 lines (31 loc) · 1.1 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
public class Solution {
public ArrayList<ArrayList<Integer>> generate(int numRows) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
if(numRows==0) return res;
ArrayList<Integer> firstRow = new ArrayList<Integer>();
firstRow.add(1);
res.add(firstRow);
if(numRows==1) return res;
ArrayList<Integer> secondRow = new ArrayList<Integer>();
secondRow.add(1);
secondRow.add(1);
res.add(secondRow);
if(numRows==2) return res;
for(int i=2;i<numRows;i++){
ArrayList<Integer> last = res.get(res.size()-1);
ArrayList<Integer> tmp = new ArrayList<Integer>();
for(int j=0;j<=last.size();j++){
if(j==0 || j==last.size()){
tmp.add(1);
}
else{
tmp.add(last.get(j-1)+last.get(j));
}
}
res.add(tmp);
}
return res;
}
}