Skip to content

Commit b429fbc

Browse files
Pascal's Triangle II
1 parent ef1e581 commit b429fbc

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

‎EASY/src/easy/PascalsTriangleII.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package easy;
2+
import java.util.*;
3+
public class PascalsTriangleII {
4+
5+
public List<Integer> getRow(int rowIndex) {
6+
if(rowIndex < 0) return new ArrayList();
7+
List<List<Integer>> result = new ArrayList();
8+
List<Integer> row = new ArrayList();
9+
row.add(1);
10+
result.add(row);
11+
for(int i = 1; i <= rowIndex; i++){
12+
List<Integer> newRow = new ArrayList();
13+
newRow.add(1);
14+
List<Integer> lastRow = result.get(i-1);
15+
for(int j = 1; j < lastRow.size(); j++){
16+
newRow.add(lastRow.get(j-1) + lastRow.get(j));
17+
}
18+
newRow.add(1);
19+
result.add(newRow);
20+
}
21+
return result.get(result.size()-1);
22+
}
23+
24+
}

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
|125|[Valid Palindrome](https://leetcode.com/problems/valid-palindrome/)|[Solution](../../blob/master/EASY/src/easy/ValidPalindrome.java)| O(n)|O(1) | Easy| Two Pointers
3333
|122|[Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)|[Solution](../../blob/master/MEDIUM/src/medium/BestTimeToBuyAndSellStockII.java)| O(n)|O(1) | Medium | Greedy
3434
|121|[Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)|[Solution](../../blob/master/EASY/src/easy/BestTimeToBuyAndSellStock.java)| O(n)|O(1) | Easy| DP
35+
|119|[Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii/)|[Solution](../../blob/master/EASY/src/easy/PascalsTriangleII.java)| O(n^2)|O(1) | Easy|
3536
|118|[Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/)|[Solution](../../blob/master/EASY/src/easy/PascalsTriangle.java)| O(n^2)|O(1) | Easy|
3637
|117|[Populating Next Right Pointers in Each Node II](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/)|[Solution](../../blob/master/HARD/src/hard/PopulatingNextRightPointersinEachNodeII.java)| O(n)|O(1) | Hard| BFS
3738
|116|[Populating Next Right Pointers in Each Node](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/)|[Solution](../../blob/master/MEDIUM/src/medium/PopulatingNextRightPointersinEachNode.java)| O(n)|O(1) | Medium| BFS

0 commit comments

Comments
 (0)