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

LeetCode – First Missing Positive (Java)

Given an unsorted integer array, find the first missing positive integer. For example, given [1,2,0] return 3 and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space. Analysis This problem can solve by using a bucket-sort like algorithm. Let’s consider finding first missing positive and 0 first. The key fact … Read more

LeetCode – Contains Duplicate II (Java)

Given an array of integers and an integer k, return true if and only if there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k. Java Solution 1 – HashMap public boolean containsNearbyDuplicate(int[] nums, int k) { HashMap<Integer, … Read more

LeetCode – Kth Largest Element in an Array (Java)

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example, given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k is always valid, 1 ≤ k ≤ array’s length. Java Solution 1 – Sorting … Read more

LeetCode – Contains Duplicate (Java)

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. Java Solution public boolean containsDuplicate(int[] nums) { if(nums==null || nums.length==0) return false;   HashSet<Integer> set = new HashSet<Integer>(); … Read more