- 博客(72)
- 收藏
- 关注
原创 [Leetcode]264. 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
2016-07-28 18:42:58 328
原创 [Leetcode]53. Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1]
2016-07-27 23:00:22 265
原创 [Leetcode]138. Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 解法1: /** * Definition for
2016-07-26 17:42:46 316
原创 [Leetcode]54. Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
2016-07-25 11:17:18 466
原创 [Leetcode]74. 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
2016-07-24 15:31:24 255
原创 [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 Tre
2016-07-24 15:26:03 268
原创 [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 larges
2016-07-21 20:04:25 250
原创 [Leetcod]154. Find Minimum in Rotated Sorted Array II
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. The array may contain duplicates. c
2016-07-21 19:35:32 197
原创 [Leetcode]153. Find Minimum in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no duplicate exists in
2016-07-21 19:12:11 212
原创 [Leetcode]162. Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in
2016-07-21 17:29:09 360
原创 [Leetcode]209. Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3
2016-07-20 21:47:28 241
原创 [Leetcode]216. Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Example 1: Inpu
2016-07-20 18:59:25 276
原创 [Leetcode]228. Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. class Solution { public: vector summaryRanges(v
2016-07-20 16:41:05 319
原创 [leetcode]229. Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. Hint: How many majority elements could it
2016-07-20 16:00:00 278
原创 [Leetcode]238. 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 ofnums except nums[i]. Solve it without division and in O(
2016-07-20 08:24:06 229
原创 [Leetcode]268. 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 sho
2016-07-19 22:05:57 223
原创 [Leetcode]287. Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number,
2016-07-19 21:52:27 219
原创 [Leetcode]289. Game of Life
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970." Given a board with m
2016-07-19 20:15:04 239
原创 [Leetcode]368. Largest Divisible Subset
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of elements in this subset satisfies: Si % Sj = 0 or Sj % Si = 0. If there are multiple solution
2016-07-18 15:45:21 230
原创 [Leetcode]322. Coin Change
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money
2016-07-18 12:23:37 227
原创 [Leetcode]Guess Number Higher or Lower II
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I'll tell you whether the number I pi
2016-07-18 10:19:19 491
原创 [Leetcode]165. Compare Version Numbers
Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and co
2016-07-17 20:35:17 217
原创 [Leetcode]189. Rotate Array
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. class Solution { public: void rotate(vect
2016-07-17 17:07:36 417
原创 [Leetcode]278. 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
2016-07-17 16:13:00 237
原创 [Leetcode]7. Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before c
2016-07-16 19:06:05 190
原创 [Leetcode]125. Valid Palindrome
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
2016-07-16 17:32:56 212
原创 [Leetcode]204. Count Primes
Description: Count the number of prime numbers less than a non-negative number, n. class Solution { public: int countPrimes(int n) { bool isPrime[n]; for (int i = 2; i < n; +
2016-07-16 17:06:46 181
原创 [Leetcode]303. 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 sumRan
2016-07-15 19:25:40 184
原创 [Leetcode]28. Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. class Solution { public: int strStr(string haystack, string need
2016-07-15 18:18:34 189
原创 [Leetcode]67. Add Binary
Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". class Solution { public: string addBinary(string a, string b) { stri
2016-07-15 17:11:11 180
原创 [Leetcode]14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. class Solution { public: string longestCommonPrefix(vector& strs) { if (strs.empty()) re
2016-07-15 16:34:15 170
原创 [Leetcode]190. Reverse Bits
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 001110010
2016-07-15 15:28:39 260
原创 [Leetcode]58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is
2016-07-14 22:12:05 225
原创 [Leetcode]374. Guess Number Higher or Lower
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I'll tell you whether the number is h
2016-07-14 21:45:56 252
原创 [Leetcode]38. Count and Say
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21. 21 is read off as
2016-07-14 21:18:58 154
原创 [Leetcode]257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-leaf paths are: ["1->2->5", "1->3"] /** * De
2016-07-14 17:13:05 174
原创 [Leetcode]290. Word Pattern
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.
2016-07-14 15:55:37 354
原创 [Leetcode]219. Contains Duplicate II
Given an array of integers and an integer k, find out whether 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. c
2016-07-14 15:09:45 211
原创 [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 tota
2016-07-13 19:49:36 284
原创 [Leetcode]205. Isomorphic Strings
Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of characters. No two c
2016-07-13 19:30:37 227
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人