LeetCode – Minimum Path Sum (Java)

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Java Solution 1: Depth-First Search A native solution would be depth-first search. It’s time is too expensive and fails the online judgement. public int minPathSum(int[][] grid) … Read more

LeetCode – Reverse Linked List (Java)

Reverse a singly linked list. Java Solution 1 – Iterative public ListNode reverseList(ListNode head) { if(head==null||head.next==null) return head;   ListNode p1 = head; ListNode p2 = p1.next;   head.next = null; while(p1!=null&& p2!=null){ ListNode t = p2.next; p2.next = p1; p1 = p2; p2 = t; }   return p1; }public ListNode reverseList(ListNode head) { … Read more

LeetCode – Isomorphic Strings (Java)

Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. For example,”egg” and “add” are isomorphic, “foo” and “bar” are not. Java Solution 1 We can define a map which tracks the char-char mappings. If a value is already … Read more

LeetCode – Implement Trie (Prefix Tree) (Java)

Implement a trie with insert, search, and startsWith methods. Java Solution 1 A trie node should contains the character, its children and the flag that marks if it is a leaf node. You can use the trie in the following diagram to walk though the Java solution. class TrieNode { char c; HashMap<Character, TrieNode> children … Read more

LeetCode – Swap Nodes in Pairs (Java)

Given a linked list, swap every two adjacent nodes and return its head. For example, given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed. Java Solution 1 Use two template variable to track … Read more

LeetCode – Count Primes (Java)

Count the number of prime numbers less than a non-negative number, n Java Solution 1 This solution exceeds time limit. public int countPrimes(int n) { n = n-1;   ArrayList<Integer> primes = new ArrayList<Integer>();   if(n<=1) return 0; if(n==2) return 1; if(n==3) return 2;   primes.add(2); primes.add(3);   for(int i=4; i<=n; i++){ boolean isPrime = … Read more

LeetCode – Simplify Path (Java)

Given an absolute path for a file (Unix-style), simplify it. For example, path = “/home/”, => “/home” path = “/a/./b/../../c/”, => “/c” path = “/../”, => “/” path = “/home//foo/”, => “/home/foo” Java Solution public String simplifyPath(String path) { Stack<String> stack = new Stack<String>();   //stack.push(path.substring(0,1));   while(path.length()> 0 && path.charAt(path.length()-1) ==’/’){ path = path.substring(0, … Read more

LeetCode – Combination Sum II (Java)

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used ONCE in the combination. Note: 1) All numbers (including target) will be positive integers. 2) Elements in a combination (a1, a2, … … Read more

LeetCode – Binary Tree Level Order Traversal II (Java)

Given a binary tree, return the bottom-up level order traversal of its nodes’ values. For example, given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 return its level order traversal as [[15,7], [9,20],[3]] Java Solution public List<ArrayList<Integer>> levelOrderBottom(TreeNode root) { ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();   if(root == null){ return result; … Read more

LeetCode – Binary Tree Level Order Traversal (Java)

Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 return its level order traversal as [[3], [9,20], [15,7]] Java Solution 1 It is obvious that this problem can … Read more