-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode-118-Pascals-Triangle.java
More file actions
36 lines (30 loc) · 1007 Bytes
/
LeetCode-118-Pascals-Triangle.java
File metadata and controls
36 lines (30 loc) · 1007 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
36
/*
LeetCode: https://leetcode.com/problems/pascals-triangle/
LintCode: http://www.lintcode.com/problem/pascals-triangle/
JiuZhang: http://www.jiuzhang.com/solutions/pascals-triangle/
ProgramCreek: http://www.programcreek.com/2014/03/leetcode-pascals-triangle-java/
Analysis:
*/
public class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(numRows<1){
return res;
}
List<Integer> pre = new ArrayList<Integer>();
pre.add(1);
res.add(pre);
for(int i = 1;i<numRows;i++){
List<Integer> cur = new ArrayList<Integer>();
cur.add(1);
for(int j = 0;j<pre.size()-1;j++){
cur.add(pre.get(j)+pre.get(j+1));
}
cur.add(1);
res.add(cur);
pre=cur;
cur=new ArrayList<Integer>();
}
return res;
}
}