Skip to content

Commit ef1e581

Browse files
Pascal's triangle
1 parent df480fc commit ef1e581

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

‎EASY/src/easy/PascalsTriangle.java

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

‎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+
|118|[Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/)|[Solution](../../blob/master/EASY/src/easy/PascalsTriangle.java)| O(n^2)|O(1) | Easy|
3536
|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
3637
|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
3738
|111|[Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/)|[Solution](../../blob/master/EASY/src/easy/MinimumDepthofBinaryTree.java)| O(n)|O(1)~O(h) | Easy| BFS, DFS

0 commit comments

Comments
 (0)