- 博客(78)
- 收藏
- 关注
原创 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
2016-10-15 16:04:24 198
转载 Partition Equal Subset Sum
Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.Note: Both the array size and ea
2016-10-12 18:15:10 338
转载 Is Subsequence
Given a string s and a string t, check if s is subsequence of t.You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, an
2016-10-12 13:48:59 190
转载 文章标题
【转】http://www.cnblogs.com/saltless/archive/2010/08/05/1793322.html 有人看到“二分答案”这个题目,可能会很不解。题目过程可以二分,答案怎么也能二分呢? 事实上,当你看到找极大值中的最小值或者求极小值中的最大值的题目时,二分答案或许是一个不错的选择。根据数据范围判断是否选用二分(二分能把时间复杂度降到log n),再和上文所说的条件
2016-09-25 11:30:09 193
转载 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 picked is highe
2016-09-25 10:58:29 283
转载 Reconstruct Itinerary
Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. Thus, the
2016-09-06 16:55:03 255
原创 Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray
2016-09-05 21:24:45 167
原创 Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.For example, given s = “leetcode”, dict = [“leet”, “co
2016-09-05 17:05:01 184
原创 Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.解: 2分搜索的应用,题目很简单,但要注意overflow的坑class Solution {public: int mySqrt(int x) { int l=0; int r=x; while(l<r)
2016-09-04 19:12:43 178
原创 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 c
2016-09-04 17:45:31 149
原创 Maximal Square
Given a 2D binary matrix filled with 0’s and 1’s, find the largest square containing only 1’s and return its area.For example, given the following matrix:1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Ret
2016-09-03 22:47:14 175
转载 Minimum Height Trees
For a undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible rooted trees, those with minimum height are called minim
2016-09-03 20:32:49 269
原创 3sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note: The solution set must not contain duplic
2016-09-02 17:48:59 191
转载 Water and Jug Problem
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply available. You need to determine whether it is possible to measure exactly z litres using these two ju
2016-08-14 19:26:03 259
转载 Linked List Random Node
Given a singly linked list, return a random node’s value from the linked list. Each node must have the same probability of being chosen.Follow up: What if the linked list is extremely large and its le
2016-08-14 19:03:52 267
原创 Kth Smallest Element in a Sorted Matrix
Note that it is the kth smallest element in the sorted order, not the kth distinct element.Example:matrix = [ [ 1, 5, 9], [10, 11, 13], [12, 13, 15] ], k = 8,return 13. Note: You may
2016-08-07 21:28:00 299 5
转载 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-05-06 19:08:25 191
原创 Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.Note: next() and hasN
2016-05-05 11:06:28 161
转载 Subsets
Given a set of distinct integers, nums, return all possible subsets.Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If
2016-04-27 13:57:34 209
转载 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, return
2016-04-27 12:14:56 246
原创 Pow(x, n)
Implement pow(x, n).解: 题目要求无比简单 但其实有很多边界条件需要考虑。。 而且对于一些特殊情况如果不优化的话分分钟超时。。class Solution {public: double myPow(double x, int n) { if(n==0)return 1; bool flag=false;//指数n是否为负数
2016-04-25 16:22:13 191
转载 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
2016-03-30 16:54:24 182
原创 Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence.For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101], therefore
2016-03-30 15:14:48 177
转载 Permutations
Given a collection of distinct 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].解:class Solutio
2016-03-29 18:03:46 156
转载 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 by n ce
2016-03-28 22:14:55 196
转载 Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers 0, 1,
2016-03-27 16:11:21 135
转载 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
2016-03-24 12:38:02 153
转载 Reverse Words in a String
Given an input string, reverse the string word by word.For example, Given s = “the sky is blue”, return “blue is sky the”.Update (2015-02-12): For C programmers: Try to solve it in-place in O(1) spa
2016-03-24 12:05:26 274
转载 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:“((()))”, “(()())”, “(())()”, “()(())”, “()()()”解: 其实
2016-03-22 18:26:35 168
转载 House Robber III
he thief has found himself a new place for his thievery again. There is only one entrance to this area, called the “root.” Besides the root, each house has one and only one parent house. After a tour,
2016-03-21 22:27:40 214
转载 Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST’s total elements.Follow up: What if the BST is modi
2016-03-21 21:57:46 158
转载 Maximum Product of Word Lengths
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case lette
2016-03-20 17:24:15 230
转载 Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.解: 实际上就是找1-n的数字中值为5的factor的个数class Solution {public: int trailingZeroes
2016-03-20 15:39:15 152
原创 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 line
2016-03-13 18:40:47 173
转载 leetcode--Bulb Switcher
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it’s off or turning off i
2016-03-13 18:24:38 219
转载 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
2016-03-12 21:54:25 142
转载 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,
2016-03-12 21:30:41 192
转载 leetcode--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].Note: Try to come up as many solutions as you can, ther
2016-03-11 22:23:43 145
转载 leetcode--Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the
2016-03-08 21:11:30 166
转载 leetcode--Count Primes
Description:Count the number of prime numbers less than a non-negative number, n.解: 筛选法解很容易想到但题目判定时间意外地很严,那就要求不断剪枝了from math import sqrt as sqclass Solution(object): def countPrimes(self, n):
2016-03-08 14:40:03 208
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人