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 – Binary Tree Longest Consecutive Sequence (Java)

Given a binary tree, find the length of the longest consecutive sequence path. The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from parent to child (cannot be the reverse). Java Solution 1 – BFS … 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 – Maximum Product of Word Lengths (Java)

Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0. Java Solution public int maxProduct(String[] words) { if(words==null || words.length==0) return 0;   … Read more

LeetCode – Range Sum Query – Mutable (Java)

Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. The update(i, val) function modifies nums by updating the element at index i to val. Java Solution 1 – Segment Tree class TreeNode{ int start; int end; int sum; TreeNode leftChild; TreeNode rightChild;   public … Read more

LeetCode – Range Sum Query 2D – Immutable (Java)

Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). Analysis Since the assumption is that there are many calls to sumRegion method, we should use some extra space to store the intermediate results. The solution is … 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

LeetCode – Letter Combinations of a Phone Number (Java)

Given a digit string, return all possible letter combinations that the number could represent. (Check out your cellphone to see the mappings) Input:Digit string “23”, Output: [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”]. Java Solution 1 – DFS This problem can be solves by a typical DFS algorithm. DFS problems are very similar … Read more