Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 98c8ebf

Browse files
edit 119
1 parent 9636518 commit 98c8ebf

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ Your ideas/fixes/algorithms are more than welcome!
478478
|122|[Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_122.java)| O(n)|O(1) | Medium | Greedy
479479
|121|[Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_121.java)| O(n)|O(1) | Easy| DP
480480
|120|[Triangle](https://leetcode.com/problems/triangle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_120.java)| O(m*n)|O(n) | Medium| DP
481-
|119|[Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_119.java)| O(n^2)|O(1) | Easy|
481+
|119|[Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_119.java)| O(n^2)|O(k) | Easy|
482482
|118|[Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_118.java)| O(n^2)|O(1) | Easy|
483483
|117|[Populating Next Right Pointers in Each Node II](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_117.java)| O(n)|O(1) | Medium| BFS
484484
|116|[Populating Next Right Pointers in Each Node](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_116.java)| O(n)|O(1) | Medium| BFS
Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,51 @@
11
package com.fishercoder.solutions;
22
import java.util.*;
3-
/**Given an index k, return the kth row of the Pascal's triangle.
3+
4+
/**
5+
* 119. Pascal's Triangle II
6+
* Given an index k, return the kth row of the Pascal's triangle.
47
58
For example, given k = 3,
69
Return [1,3,3,1].
710
811
Note:
9-
Could you optimize your algorithm to use only O(k) extra space?*/
12+
Could you optimize your algorithm to use only O(k) extra space?
13+
*/
1014

1115
public class _119 {
1216

13-
public List<Integer> getRow(int rowIndex) {
14-
if(rowIndex < 0) return new ArrayList();
15-
List<List<Integer>> result = new ArrayList();
16-
List<Integer> row = new ArrayList();
17-
row.add(1);
18-
result.add(row);
19-
for(int i = 1; i <= rowIndex; i++){
20-
List<Integer> newRow = new ArrayList();
21-
newRow.add(1);
22-
List<Integer> lastRow = result.get(i-1);
23-
for(int j = 1; j < lastRow.size(); j++){
24-
newRow.add(lastRow.get(j-1) + lastRow.get(j));
17+
public static class Solution1 {
18+
public List<Integer> getRow(int rowIndex) {
19+
if (rowIndex < 0) return new ArrayList();
20+
List<List<Integer>> result = new ArrayList();
21+
List<Integer> row = new ArrayList();
22+
row.add(1);
23+
result.add(row);
24+
for (int i = 1; i <= rowIndex; i++) {
25+
List<Integer> newRow = new ArrayList();
26+
newRow.add(1);
27+
List<Integer> lastRow = result.get(i - 1);
28+
for (int j = 1; j < lastRow.size(); j++) {
29+
newRow.add(lastRow.get(j - 1) + lastRow.get(j));
30+
}
31+
newRow.add(1);
32+
result.add(newRow);
33+
}
34+
return result.get(result.size() - 1);
35+
}
36+
}
37+
38+
public static class Solution_OkSpace {
39+
public List<Integer> getRow(int rowIndex) {
40+
List<Integer> row = new ArrayList<>();
41+
for (int i = 0; i < rowIndex + 1; i++) {
42+
row.add(0, 1);
43+
for (int j = 1; j < row.size() - 1; j++) {
44+
row.set(j, row.get(j) + row.get(j + 1));
45+
}
2546
}
26-
newRow.add(1);
27-
result.add(newRow);
47+
return row;
2848
}
29-
return result.get(result.size()-1);
3049
}
3150

3251
}

0 commit comments

Comments
 (0)