This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.InputStream; | |
import java.util.HashSet; | |
import java.util.LinkedHashSet; | |
import java.util.PriorityQueue; | |
import java.util.Scanner; | |
import java.util.Set; | |
import java.util.TreeSet; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public List<List<Integer>> pathSum(TreeNode root, int sum) { | |
List<List<Integer>> paths = new ArrayList<List<Integer>>(); | |
if(root == null){ | |
return paths; | |
}else{ | |
List<Integer> path = new ArrayList<Integer>(); | |
getpaths(root, path, paths, 0, sum); | |
return paths; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static ListNode findmid(ListNode cur){ | |
ListNode s = cur; | |
ListNode f = cur; | |
while(f != null){ | |
f = f.next; | |
if(f != null && f.next != null){ | |
f = f.next; | |
s = s.next; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static ListNode reverse(ListNode head) { | |
ListNode cur = head; | |
ListNode t = cur.next; | |
ListNode n = null; | |
if(t != null){ | |
n = t.next; | |
t.next = cur; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public List<Integer> postorderTraversal(TreeNode root) { | |
Stack<TreeNode> s = new Stack<TreeNode>(); | |
List<Integer> l = new ArrayList<Integer>(); | |
if(root == null){ | |
return l; | |
} | |
s.push(root); | |
TreeNode cur = null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int minPathSum(int[][] grid) { | |
int m = grid.length; | |
int n = grid[0].length; | |
for(int i = 1; i < m; i++){ | |
grid[i][0] = grid[i-1][0] + grid[i][0]; | |
} | |
for(int i = 1; i < n; i++){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int longestConsecutive(int[] num) { | |
//need support for concurrent modifications of the backing table | |
//@todo find a different way of removing entries from the table during iteration | |
Map<String, Set<Integer>> h = new ConcurrentHashMap<String, Set<Integer>>(); | |
int max = Integer.MIN_VALUE; | |
//init the table | |
for(int v : num){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private final Stack<Integer> s; | |
private Integer min; | |
public MinStack() { | |
this.s = new Stack<Integer>(); | |
this.min = null; | |
} | |
public Integer push(Integer e) { | |
if (min == null) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void reorderList(ListNode head) { | |
if(head == null || head.next == null){ | |
return; | |
} | |
ListNode cur = head; | |
ListNode sh = findmid(cur); | |
ListNode shr = reverse(sh.next); | |
sh.next = null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int maxProduct(int[] A) { | |
int n = A.length; | |
int max = Integer.MIN_VALUE; | |
int np = 1; | |
int pp = 1; | |
for(int i = 0; i < n; i++){ | |
if(A[i] == 0){ |