
LeetCode题解
文章平均质量分 74
LeetCode.com于2013年底更新,共计151题,基本涵盖了常见的面试题目。
Allanxl
这个作者很懒,什么都没留下…
展开
-
LeetCode | 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".思路:方法1:首先把句子看做由词组成的,例如“A B C”,因此可以将句子的所有字符前后交换,得到“C原创 2014-08-25 22:22:31 · 24276 阅读 · 13 评论 -
LeetCode | Rotate Image
题目:You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?思路:将matrix中的结点考虑为空间向量。顺时针转动90度就是每个原创 2013-12-23 10:06:00 · 4831 阅读 · 2 评论 -
LeetCode | Valid Number
题目:Validate if a given string is numeric.Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => trueNote: It is intended for the problem statement原创 2013-09-15 00:25:46 · 3822 阅读 · 0 评论 -
LeetCode | 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 seq原创 2013-09-29 10:56:11 · 3570 阅读 · 1 评论 -
LeetCode | Integer to Roman
题目:Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.思路:基本思路是将整数按照10进制存到一个数组中,针对每个数根据>5,=5,代码:class Solution {原创 2013-09-15 10:31:40 · 3291 阅读 · 3 评论 -
LeetCode | Roman to Integer
题目:Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.思路:罗马数字的基本思想是右加左减,并且左减最大只能一位。所以我们可以将所以数相加,然后再遍历一次,当前一个数比后一个数小时,从结果中减去该数原创 2013-09-15 11:20:42 · 3664 阅读 · 0 评论 -
LeetCode | First Missing Positive
题目: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 us原创 2013-09-15 00:54:23 · 2198 阅读 · 0 评论 -
LeetCode | Clone Graph
题目:Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.OJ's undirected graph serialization:Nodes are labeled uniquely.We use # as a separat原创 2014-01-03 22:42:29 · 11294 阅读 · 0 评论 -
LeetCode | Surrounded Regions
题目:Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.A region is captured by flipping all 'O's into 'X's in that surrounded region .For example,X X X原创 2014-01-04 13:21:00 · 6913 阅读 · 6 评论 -
LeetCode | 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.思路:普通的链表复制就原创 2014-01-03 22:06:42 · 5149 阅读 · 0 评论 -
LeetCode | Candy
经典LeetCode问题,找到一个更加简单的方法~原创 2014-01-02 10:49:16 · 7954 阅读 · 0 评论 -
LeetCode | Trapping Rain Water
题目: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原创 2013-12-31 16:35:53 · 3597 阅读 · 0 评论 -
LeetCode | Substring with Concatenation of All Words
题目:You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and w原创 2013-12-31 16:17:07 · 3758 阅读 · 0 评论 -
LeetCode| Scramble String
题目:Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation of s1 = "great": great /原创 2013-12-31 15:24:51 · 3972 阅读 · 0 评论 -
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原创 2013-12-31 13:56:13 · 2119 阅读 · 0 评论 -
LeetCode | Text Justification
题目:Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.You should pack your words in a greedy app原创 2013-12-27 01:00:14 · 2109 阅读 · 1 评论 -
LeetCode | 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 rea原创 2013-12-26 23:02:28 · 2287 阅读 · 3 评论 -
LeetCode | Combination Sum II
题目:Given a collection 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 th原创 2013-12-26 22:45:34 · 2280 阅读 · 0 评论 -
LeetCode | Combination Sum
题目: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 unlim原创 2013-12-26 22:34:28 · 2252 阅读 · 0 评论 -
leetCode | Next Permutation
题目: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原创 2013-12-26 22:14:05 · 4794 阅读 · 0 评论 -
LeetCode | Divide Two Integers
题目:Divide two integers without using multiplication, division and mod operator.思路:1)考虑边界问题。2)考虑INT_MIN与INT_MAX绝对值之间差1。3)考虑符号。类似http://blog.csdn.net/lanxu_yy/article/details/11686原创 2013-12-26 21:32:32 · 2072 阅读 · 0 评论 -
LeetCode | String to Integer (atoi)
题目:Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible原创 2013-12-26 20:50:39 · 1960 阅读 · 0 评论 -
LeetCode | Word Ladder II
题目:Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:Only one letter can be changed at a timeEach intermediate word原创 2014-01-03 13:59:26 · 4472 阅读 · 0 评论 -
LeetCode | Word Ladder
题目:Given two words (start and end), and a dictionary, find the length of shortest transformation sequence fromstart to end, such that:Only one letter can be changed at a timeEach intermediat原创 2013-12-26 17:52:39 · 1983 阅读 · 1 评论 -
LeetCode | Implement strStr()
题目:Implement strStr().Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.思路:BF或者KMP算法。BF无法通过时间复杂度要求。代码:class S原创 2013-12-26 14:24:03 · 2695 阅读 · 0 评论 -
LeetCode | Minimum Window Substring
题目:Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S = "ADOBECODEBANC"T = "ABC"Minimum windo原创 2013-12-26 12:43:54 · 1713 阅读 · 3 评论 -
LeetCode | Multiply Strings
题目:Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-negative.思路:模拟乘法的过程。首先第二个数的每一原创 2013-12-25 17:07:03 · 1425 阅读 · 0 评论 -
LeetCode | Merge Intervals
题目:Given a collection of intervals, merge all overlapping intervals.For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18].思路:首先按照start来排序,然后依次合并相邻的重叠项。原创 2013-12-25 16:43:17 · 1472 阅读 · 0 评论 -
LeetCode | Insert Interval
题目:Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start ti原创 2013-12-25 16:15:46 · 1523 阅读 · 0 评论 -
LeetCode | Distinct Subsequences
题目:Given a string S and a string T, count the number of distinct subsequences of T in S.A subsequence of a string is a new string which is formed from the original string by deleting some (c原创 2013-12-25 13:00:51 · 2774 阅读 · 0 评论 -
LeetCode | Maximal Rectangle
题目:Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.思路:基本思路是循环确定一个左上角的点与右下角的点,然后判断该区域是否都为’1‘。如下图:为了循环利用,我们可以采用动态原创 2013-12-24 17:32:55 · 5768 阅读 · 0 评论 -
LeetCode | Spiral Matrix II
题目:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.For example,Given n = 3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9原创 2013-12-24 14:26:04 · 1442 阅读 · 0 评论 -
LeetCode | 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原创 2013-12-24 14:15:25 · 1869 阅读 · 0 评论 -
LeetCode | 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,−原创 2013-12-24 13:12:34 · 3424 阅读 · 1 评论 -
LeetCode | Combinations
题目:Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3],原创 2013-12-24 11:32:20 · 1367 阅读 · 0 评论 -
LeetCode | Permutations II
题目:Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and原创 2013-12-24 11:07:35 · 1124 阅读 · 0 评论 -
LeetCode | 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].原创 2013-12-24 10:47:09 · 1505 阅读 · 0 评论 -
LeetCode | Binary Tree Zigzag Level Order Traversal
题目:Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Give原创 2013-12-24 10:39:51 · 1485 阅读 · 0 评论 -
LeetCode | ZigZag Conversion
题目:The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA原创 2013-12-24 10:23:40 · 2003 阅读 · 2 评论 -
LeetCode | Unique Paths II
题目:Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectively原创 2013-12-23 18:25:41 · 1606 阅读 · 0 评论