Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1. Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring. Solution: public class Solution { public int
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Solution 1: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode dummy = new ListNode(0); ListNode scan=dummy, scanL1=l1, scanL2=l2; int carry = 0; while(scanL1!=null && scanL2!=null){ int digit = (scanL1.val+scanL2.val+carry)%10; carry = (scanL1.val+scanL2.val+carry)/10; scan.next = new ListNode(digit); scanL1 = scanL1.next; scanL2 = scanL2.next; scan = scan.next; } scanL1 = scanL1==null ?
Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. Solution: public class Solution { public int[] twoSum(int[] nums, int target) { Map<Integer,Integer> map =
Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = “aab”, Return [ ["aa","b"], ["a","a","b"] ] Solution: public class Solution { public List<List<String>> partition(String s) { List<List<String>> ans = new ArrayList<>(); if(s.length()==0){ List<String> list = new ArrayList<>(); ans.add(list); return ans; } for(int i=0; i<s.length(); i++){ String s1 = s.substring(0,i+1); String s2 =
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, “A man, a plan, a canal: Panama” is a palindrome. “race a car” is not a palindrome. Solution: public class Solution { public boolean isPalindrome(String s) { if(s.length()==0) return true; int l=0, r=s.length()-1; s = s.toLowerCase(); /* pay attention to usage of toLowerCase() function */ while(l<r){ while(l<s.length() && !isAlphanumber(s.charAt(l))) l++; while(r>=0 &&
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. Solution: public class Solution { public String longestPalindrome(String s) { int l=0, r=0; int maxHalf = 0; for(int i=0, half=0; i<s.length(); i++){ half = 0; while(i-half>=0 && i+half<s.length()){ if(s.charAt(i-half)!=s.charAt(i+half)) break; half++; } /* "<=" instead of "<" */ if(maxHalf<=half){ maxHalf
Determine whether an integer is a palindrome. Do this without extra space. Solution: public class Solution { public boolean isPalindrome(int x) { String str = String.valueOf(x); int left = 0; int right = str.length()-1; while(left<right){ if(str.charAt(left++)!=str.charAt(right--)) return false; } return true; } }
Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. For example, Given heights = [2,1,5,6,2,3], return 10. Solution: public class Solution { public int largestRectangleArea(int[] heights) { Stack<Integer> stack = new Stack<>(); int maxArea = 0; int i=0; /* DO NOT use for-loop */ while(i<heights.length){ if(stack.isEmpty()||heights[stack.peek()]<=heights[i]){ stack.push(i++); } else { int h =
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. Solution 1: $O(n)$ time, $O(n)$ space public class Solution { public int trap(int[] height) { if(height.length==0) return 0; int[] left = new int[height.length]; int[] right = new int[height.length]; int curMax = 0; for(int i=0; i<height.length; i++){ if(height[i]>curMax)