LeetCode – Word Ladder II (Java)

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: 1) Only one letter can be changed at a time, 2) Each intermediate word must exist in the dictionary. For example, given: start = “hit”, end = “cog”, and dict = [“hot”,”dot”,”dog”,”lot”,”log”], return: [ [“hit”,”hot”,”dot”,”dog”,”cog”], … Read more

LeetCode – Contains Duplicate III (Java)

Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k. Java Solution 1 – Simple This solution simple. Its time complexity is O(nlog(k)). … Read more

LeetCode – Count Complete Tree Nodes (Java)

Given a complete binary tree, count the number of nodes. The solution to this problem can be as simple as the following: public int countNodes(TreeNode root) { if(root == null){ return 0; }   return 1 + countNodes(root.left) + countNodes(root.right); }public int countNodes(TreeNode root) { if(root == null){ return 0; } return 1 + countNodes(root.left) … Read more

LeetCode – Construct Binary Tree from Preorder and Inorder Traversal (Java)

Given preorder and inorder traversal of a tree, construct the binary tree. Analysis Consider the following example: in-order: 4 2 5 (1) 6 7 3 8 pre-order: (1) 2 4 5 3 7 6 8 From the pre-order array, we know that first element is the root. We can find the root in in-order array. … Read more

LeetCode – Course Schedule II (Java)

This is an extension of Course Schedule. This time a valid sequence of courses is required as output. Analysis If we use the BFS solution of Course Schedule, a valid sequence can easily be recorded. Java Solution public int[] findOrder(int numCourses, int[][] prerequisites) { if(prerequisites == null){ throw new IllegalArgumentException("illegal prerequisites array"); }   int … Read more

LeetCode – Reverse Linked List II (Java)

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Analysis Java Solution public ListNode reverseBetween(ListNode head, int m, int n) { if(m==n) return head;   ListNode prev = null;//track (m-1)th node ListNode first = new ListNode(0);//first’s … Read more

LeetCode – Next Permutation (Java)

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replacement must be in-place, do not allocate extra memory. Here are some examples. Inputs are in the left-hand column and … Read more

LeetCode – Restore IP Addresses (Java)

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example: given “25525511135”,return [“255.255.11.135”, “255.255.111.35”]. Java Solution This is a typical search problem and it can be solved by using DFS. public List<String> restoreIpAddresses(String s) { ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>(); ArrayList<String> t = new ArrayList<String>(); dfs(result, s, … Read more

LeetCode – Longest Valid Parentheses (Java)

Given a string containing just the characters ‘(‘ and ‘)’, find the length of the longest valid (well-formed) parentheses substring. For “(()”, the longest valid parentheses substring is “()”, which has length = 2. Another example is “)()())”, where the longest valid parentheses substring is “()()”, which has length = 4. Java Solution A stack … Read more

LeetCode – Remove Duplicates from Sorted List II (Java)

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, given 1->1->1->2->3, return 2->3. Java Solution public ListNode deleteDuplicates(ListNode head) { ListNode t = new ListNode(0); t.next = head;   ListNode p = t; while(p.next!=null&&p.next.next!=null){ if(p.next.val == p.next.next.val){ int dup = p.next.val; while(p.next!=null&&p.next.val==dup){ … Read more

LeetCode – Search in Rotated Sorted Array II (Java)

Follow up for “Search in Rotated Sorted Array“: what if duplicates are allowed? Write a function to determine if a given target is in the array. Java Solution public boolean search(int[] nums, int target) { int left=0; int right=nums.length-1;   while(left<=right){ int mid = (left+right)/2; if(nums[mid]==target) return true;   if(nums[left]<nums[mid]){ if(nums[left]<=target&& target<nums[mid]){ right=mid-1; }else{ left=mid+1; … Read more

LeetCode – Maximal Square (Java)

Given a 2D binary matrix filled with 0’s and 1’s, find the largest square containing all 1’s and return its area. For example, given the following matrix: 1101 1101 1111 Return 4. Analysis This problem can be solved by dynamic programming. The changing condition is: t[i][j] = min(t[i][j-1], t[i-1][j], t[i-1][j-1]) + 1. It means the … Read more