-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode-119-Pascals-Triangle-II.java
More file actions
49 lines (37 loc) · 1.31 KB
/
LeetCode-119-Pascals-Triangle-II.java
File metadata and controls
49 lines (37 loc) · 1.31 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
LeetCode: https://leetcode.com/problems/pascals-triangle-ii/
LintCode: http://www.lintcode.com/problem/pascals-triangle-ii/
JiuZhang: http://www.jiuzhang.com/solutions/pascals-triangle-ii/
ProgramCreek: http://www.programcreek.com/2014/04/leetcode-pascals-triangle-ii-java/
Analysis:
*/
public class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> result = new ArrayList<Integer>();
if(rowIndex < 0) return result;
result.add(1);
for(int i = 1; i <= rowIndex; i++){
result.add(1);
for(int j = 0; j < i - 1; j++){
result.add(result.get(0) + result.get(1));
result.remove(0);
}
result.remove(0);
result.add(1);
}
return result;
}
public List<Integer> getRow(int rowIndex) {
List<Integer> result = new ArrayList<>();
result.add(1);
for (int i = 1; i <= rowIndex; i++) {
List<Integer> curr = new ArrayList<>();
curr.add(1);
for (int j = 0; j < i - 1; j++) {
curr.add(result.get(j) + result.get(j + 1));
}
curr.add(1);
result = curr;
}
return result;
}}