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

LeetCode – Trapping Rain Water (Java)

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. Analysis This problem is similar to Candy. It can be solve by scanning from both sides and then get the total. Java … Read more

LeetCode – Word Search (Java)

Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once. For example, given board = [ [“ABCE”], [“SFCS”], [“ADEE”] … Read more

LeetCode – Shortest Palindrome (Java)

Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation. For example, given “aacecaaa”, return “aaacecaaa”; given “abcd”, return “dcbabcd”. Java Solution 1 public String shortestPalindrome(String s) { int i=0; int j=s.length()-1; … Read more