Trapping Rain Water

2016-01-26
阅读 2 分钟
3.2k
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.

First Missing Positive

2016-01-24
阅读 2 分钟
2.7k
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space.

Combination Sum II

2016-01-23
阅读 2 分钟
2.8k
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be positive integers. Elements in a combination (a1, a2, … , ...

Combination Sum

2016-01-19
阅读 2 分钟
3.5k
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) will be positive integers. Elements in a combination...

Count and Say

2016-01-18
阅读 2 分钟
4.2k
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 "one 2, then one 1" or 1211.Given an integer n, generate the nth sequence. Note: The sequence of integers will...

Sudoku Solver

2016-01-17
阅读 3 分钟
3.6k
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red.

Valid Sudoku

2016-01-14
阅读 2 分钟
4.2k
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'.

Search Insert Position

2016-01-12
阅读 2 分钟
2.3k
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 → 1[1,3,5,6], 7 → 4[1,3,5,6], 0 → 0

Search for a Range

2016-01-11
阅读 2 分钟
2.7k
Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the target is not found in the array, return [-1, -1]. For example,Given [5, 7, 7, 8, 8, 10] and target value 8,return [3, 4].

# Search in Rotated Sorted Array

2016-01-11
阅读 2 分钟
2.7k
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). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplicate exists in the array.

Longest Valid Parentheses

2016-01-10
阅读 2 分钟
2.6k
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 "()(...

Next Permutation

2016-01-09
阅读 2 分钟
6.4k
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replacement must be in-place, do not allocate extra memo...

Substring with Concatenation of All Words

2016-01-07
阅读 2 分钟
3.1k
You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters. For example, given:s: "barfoothefoobarman"words: ["foo", "bar"]...

Divide Two Integers

2016-01-06
阅读 2 分钟
3.1k
Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT.

Implement strStr()

2016-01-05
阅读 3 分钟
3.8k
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Remove Element

2016-01-04
阅读 1 分钟
1.8k
Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length.

Reverse Nodes in k-Group

2015-12-31
阅读 3 分钟
2.9k
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nodes itself may be changed. Only constant memory ...

Swap Nodes in Pairs

2015-12-30
阅读 2 分钟
2.9k
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.

Merge k Sorted Lists

2015-12-29
阅读 3 分钟
2.8k
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

Generate Parentheses

2015-12-28
阅读 1 分钟
2.9k
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:"((()))", "(()())", "(())()", "()(())", "()()()"

Merge Two Sorted Lists

2015-12-28
阅读 2 分钟
3.7k
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

Valid Parentheses

2015-12-28
阅读 2 分钟
2k
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

Remove Nth Node From End of List

2015-12-27
阅读 2 分钟
2.3k
Given a linked list, remove the nth node from the end of list and return its head. For example, {代码...} Note:Given n will always be valid.Try to do this in one pass.

Letter Combinations of a Phone Number

2015-12-26
阅读 3 分钟
2.7k
Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below.

3Sum Closest

2015-12-24
阅读 2 分钟
2.8k
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

3Sum

2015-12-23
阅读 3 分钟
3.9k
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: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The solution set must not contain duplicate triplets.

Longest Common Prefix

2015-12-22
阅读 1 分钟
3.1k
Write a function to find the longest common prefix string amongst an array of strings.

Roman to Integer

2015-12-21
阅读 2 分钟
2.2k
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.

Integer to Roman

2015-12-20
阅读 2 分钟
2.5k
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.

Container With Most Water

2015-12-20
阅读 2 分钟
3.7k
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most wa...