LeetCode
共 155 篇文章
LeetCode 152 - Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4] , the contiguous subarray [2,3] has the largest product = 6 . public int maxProduct(int[] nums) { int result = nums[0]; int minHere = nums[0], maxHere…
LeetCode 179 - Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9] , the largest formed number is 9534330 . Note: The result may be very large, so you need to return a string instead of an integer. public String largestNumber(int[] nums)…
LeetCode 46 - Permutations
Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [1,2,3] , [1,3,2] , [2,1,3] , [2,3,1] , [3,1,2] , and [3,2,1] . public List<List<Integer>> permute(int[] num) { List<List<Integer>> result = new ArrayList<>(); perm(result, num, 0)…
2015-08-04 00:36 · LeetCode · Backtracking
LeetCode 160 - Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have no intersection at all, return null . The linked …
2015-08-03 23:25 · LeetCode · LinkedList · Linkedin
LeetCode 242 - Valid Anagram
Given two strings s and t , write a function to determine if t is an anagram of s . For example, s = "anagram", t = "nagaram", return true. s = "rat", t = "car", return false. Note: You may assume the string contains only lowercase alphabets. Solution: bool isAnagram(string s, string t) { if(s.size…
LeetCode 223 - Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. Assume that the total area is never beyond the maximum possible value of int. public int computeArea(int A, int B, int C, int D, …
2015-07-27 13:22 · LeetCode
LeetCode 232 - Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether the queue is empty. Notes: You must use only standard operations of a stack -- …
2015-07-27 13:15 · LeetCode
LeetCode 241- Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are + , - and * . Example 1 Input: "2-1-1" . ((2-1)-1) = 0 (2-(1-1)) = 2 Output: [0, 2] Example 2 Input: "2*3-4*5" (2*(3-(4*5))) = …
2015-07-27 12:56 · LeetCode
LeetCode 227 - Basic Calculator II
Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, + , - , * , / operators and empty spaces . The integer division should truncate toward zero. You may assume that the given expression is always valid. Some examples: "3+2*…
2015-07-27 12:24 · LeetCode
LeetCode 141 - Linked List Cycle
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? Solution 1: public boolean hasCycle(ListNode head) { ListNode walker = head, runner = head; do { if(runner == null || runner.next == null) return false; walker = walker.next; runner = runn…
2015-07-21 11:44 · LeetCode · LinkedList
LeetCode 106 - Construct Binary Tree from Preorder and Inorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL)…
2015-07-20 06:00 · LeetCode · BinaryTree
LeetCode 105 - Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) …
2015-07-20 05:53 · LeetCode · BinaryTree
LeetCode 233 - Number of Digit One
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example: Given n = 13, Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. Solution: For example '8192': 1-999 -> countDigitOne(999) 1000-1999 -> 100…
LeetCode 82 - Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given 1->2->3->3->4->4->5 , return 1->2->5 . Given 1->1->1->2->3 , return 2->3 . public ListNode deleteDuplicates(ListNode head) { ListNode dummy = new ListNo…
2015-06-25 14:40 · LeetCode · LinkedList
LeetCode 83 - Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once . For example, Given 1->1->2 , return 1->2 . Given 1->1->2->3->3 , return 1->2->3 . public ListNode deleteDuplicates(ListNode head) { ListNode p = head; while(p != null && p.next != null) { if(p.next.val == p.…
2015-06-25 12:28 · LeetCode · LinkedList
LeetCode 73 - Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Follow up: Did you use extra space? A straight forward solution using O( mn ) space is probably a bad idea. A simple improvement uses O( m + n ) space, but still not the best solution. Could you devise a c…
LeetCode 71 - Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/" , => "/home" path = "/a/./b/../../c/" , => "/c" Corner Cases: Did you consider the case where path = "/../" ? In this case, you should return "/" . Another corner case is the path might contain multiple slashe…
2015-06-22 16:20 · LeetCode
LeetCode 67 - Add Binary
Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100" . public String addBinary(String a, String b) { StringBuilder sb = new StringBuilder(); int m = a.length()-1, n = b.length()-1; int carry = 0; while(m >= 0 || n >= 0) { int v1 = m<0?0:a.cha…
2015-06-22 15:36 · LeetCode
LeetCode 49 - Anagrams
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. public List<String> anagrams(String[] strs) { List<String> result = new ArrayList<>(); Map<String, List<String>> map = new HashMap<>(); if(strs == null || strs.length==0) return result…
2015-06-21 14:52 · LeetCode
LeetCode - Longest Valid Parentheses
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 "(…
2015-06-18 02:16 · LeetCode