Enjoy LeetCode
LeetCode刷题记录,和大家一起学习,一起进步!
楚兴
这个作者很懒,什么都没留下…
展开
-
[LeetCode] Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.Example: Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excludi原创 2016-06-19 13:05:17 · 2684 阅读 · 0 评论 -
[LeetCode] Design Twitter
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user’s news feed. Your design should support the fol原创 2016-06-19 12:35:17 · 1560 阅读 · 0 评论 -
[LeetCode] Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection.Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].Note:Each element in the result should appear as many times as it s原创 2016-05-21 15:04:57 · 4501 阅读 · 0 评论 -
[LeetCode] Intersection of Two Arrays
Given two arrays, write a function to compute their intersection.Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].Note:Each element in the result must be unique.The result can be in a原创 2016-05-19 10:43:59 · 1272 阅读 · 0 评论 -
[LeetCode] Self Crossing
You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to the north, then x[1] metres to the west, x[2] metres to the south, x[3] metres to the east and so on. I原创 2016-05-16 21:47:19 · 4140 阅读 · 0 评论 -
[LeetCode] Palindrome Pairs
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a palindrome.Example 1: Given words =原创 2016-05-16 13:57:00 · 2949 阅读 · 0 评论 -
[LeetCode] Counting Bits
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as an array.Example: For num = 5 you sh原创 2016-05-16 11:21:20 · 3888 阅读 · 0 评论 -
[LeetCode] Flatten Nested List Iterator
Given a nested list of integers, implement an iterator to flatten it.Each element is either an integer, or a list – whose elements may also be integers or other lists.Example 1: Given the list [[1,1],原创 2016-05-16 10:54:59 · 4144 阅读 · 0 评论 -
[LeetCode] Power of Four
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.Example: Given num = 16, return true. Given num = 5, return false.Follow up: Could you solve it without loops/re原创 2016-05-16 10:22:03 · 3449 阅读 · 0 评论 -
[LeetCode] Integer Break
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.For example, given n = 2, return 1原创 2016-05-15 22:09:00 · 3860 阅读 · 0 评论 -
[LeetCode] Reverse Vowels of a String
Write a function that takes a string as input and reverse only the vowels of a string.Example 1: Given s = “hello”, return “holle”.Example 2: Given s = “leetcode”, return “leotcede”.解题思路双指针,一个往前移动,一个原创 2016-05-15 11:30:43 · 3984 阅读 · 0 评论 -
[LeetCode] Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements.For example, Given [1,1,1,2,2,3] and k = 2, return [1,2].Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elem原创 2016-05-15 10:59:59 · 4045 阅读 · 0 评论 -
[LeetCode] Increasing Triplet Subsequence
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array.Formally the function should: Return true if there exists i, j, k such that arr[i] < arr[j]原创 2016-02-16 21:39:32 · 3494 阅读 · 0 评论 -
[LeetCode] Power of Three | Power of Two
Given an integer, write a function to determine if it is a power of three.Follow up: Could you do it without using any loop / recursion?解题思路log运算。实现代码//Runtime: 18 mspublic class Solution { publi原创 2016-01-28 21:39:28 · 1988 阅读 · 0 评论 -
[LeetCode] Longest Increasing Path in a Matrix
Given an integer matrix, find the length of the longest increasing path.From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of t原创 2016-01-28 20:49:30 · 2953 阅读 · 0 评论 -
[LeetCode] Patching Array
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such that any number in range [1, n] inclusive can be formed by the sum of some elements in the array. Retur原创 2016-01-28 17:27:57 · 2763 阅读 · 1 评论 -
[LeetCode] Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.You should try to do it in plac原创 2016-01-28 16:05:54 · 1585 阅读 · 1 评论 -
[LeetCode] Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, t原创 2015-11-11 19:04:27 · 1628 阅读 · 0 评论 -
[LeetCode] Product of Array Except Self
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Solve it without division and in O(n).For e原创 2015-11-10 17:26:45 · 812 阅读 · 0 评论 -
[LeetCode] Sliding Window Maximum
Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window mo原创 2015-11-10 16:45:11 · 997 阅读 · 0 评论 -
[LeetCode] Range Sum Query - Immutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.Example: Given nums = [-2, 0, 3, -5, 2, -1] sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 su原创 2015-11-10 15:09:10 · 8336 阅读 · 0 评论 -
[LeetCode] Search a 2D Matrix II
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted in ascending from left to right.Integers in each co原创 2015-11-09 21:17:43 · 1676 阅读 · 0 评论 -
[LeetCode] Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each row is原创 2015-11-09 21:02:06 · 765 阅读 · 0 评论 -
[LeetCode] Binary Tree Paths
Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree: 1 / \2 3 \ 5All root-to-leaf paths are: [“1->2->5”, “1->3”]解题思路先序遍历,递归的过程中保存路径,遇到叶子节点时将路原创 2015-11-08 17:34:08 · 897 阅读 · 0 评论 -
[LeetCode] 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 1Input: "2原创 2015-11-09 18:08:20 · 4477 阅读 · 0 评论 -
[LeetCode] Remove Invalid Parentheses
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.Note: The input string may contain letters other than the parentheses ( and ).Examp原创 2015-11-09 16:45:57 · 8809 阅读 · 0 评论 -
[LeetCode] 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 s原创 2015-11-09 11:17:36 · 1865 阅读 · 0 评论 -
[LeetCode] Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, r原创 2015-11-08 17:06:54 · 1496 阅读 · 0 评论 -
[LeetCode] Single Number III
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.For example:Given nums = [1,原创 2015-11-08 14:59:22 · 1971 阅读 · 0 评论 -
[LeetCode] Single Number II
Given an array of integers, every element appears three times except for one. Find that single one.Note: Your algorithm should have a linear runtime complexity. Could you implement it without using ex原创 2015-11-08 14:34:19 · 1522 阅读 · 0 评论 -
[LeetCode] Single Number
Given an array of integers, every element appears twice except for one. Find that single one.Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra me原创 2015-11-08 13:48:22 · 1315 阅读 · 0 评论 -
[LeetCode] Ugly Number II
Write a program to find the n-th ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly原创 2015-11-07 16:03:46 · 2391 阅读 · 0 评论 -
[LeetCode] Ugly Number
Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it in原创 2015-11-07 15:40:56 · 1654 阅读 · 0 评论 -
[LeetCode] Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.For example, Given nums = [0, 1, 3] return 2.Note: Your algorithm should run in li原创 2015-11-07 14:59:37 · 3093 阅读 · 0 评论 -
[LeetCode] Integer to English Words
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231−12^{31} - 1.For example, 123 -> “One Hundred Twenty Three” 12345 -> “Twelve Thousa原创 2015-11-07 14:28:08 · 1954 阅读 · 0 评论 -
[LeetCode] H-Index II
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?Hint:Expected runtime complexity is in O(log n) and the input is sorted.解题思路二分法。实现代码原创 2015-11-07 13:23:58 · 2135 阅读 · 0 评论 -
[LeetCode] H-Index
Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher’s h-index.According to the definition of h-index on Wikipedia: “A scien原创 2015-11-07 12:58:23 · 1802 阅读 · 0 评论 -
[LeetCode] First Bad Version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the原创 2015-11-06 18:51:09 · 2256 阅读 · 0 评论 -
[LeetCode] Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, retu原创 2015-11-06 18:34:43 · 2416 阅读 · 0 评论 -
[LeetCode] Peeking Iterator
Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation – it essentially peek() at the element that will be return原创 2015-11-06 17:59:25 · 2028 阅读 · 0 评论