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].思路:先是进行排序,从前往后按照第一个数字进行排序。这个时候一个小技巧就是把第一个区间保存原创 2015-10-17 14:05:06 · 346 阅读 · 0 评论 -
Merge Sorted Array
题目:Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold原创 2015-10-17 13:48:10 · 311 阅读 · 0 评论 -
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 in the原创 2015-10-16 20:56:03 · 293 阅读 · 0 评论 -
Unique Paths
题目:A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the b原创 2015-10-16 20:54:11 · 272 阅读 · 0 评论 -
Pow(x, n)
题目:Implement pow(x, n).思路:递归首先把极限情况考虑,直接返回,接下来就是负指数的处理。最后考虑下指数是奇数还是偶数,递归调用。(注意,这种类型题目务必判断是否越界)代码:class Solution {public: double myPow(double x, int n) {原创 2015-10-02 07:35:17 · 313 阅读 · 0 评论 -
Divide Two Integers
题目:Divide two integers without using multiplication, division and mod operator.思路:注意越界问题。比如: -2147483648/1=-2147483648 -2147483648/-1=2147483647最主要的应该是如何进行除法吧,思路如下:a/b,a-b -- a-2*b --原创 2015-10-02 12:46:06 · 339 阅读 · 0 评论 -
Sqrt(x)
题目:计算X均方根思路:二分法代码:class Solution {public: int mySqrt(int x) { if(x<=0) return 0; if(x<4) return 1; if(x/2>=46340){ return binaryLowInt(x原创 2015-09-28 21:40:47 · 499 阅读 · 0 评论 -
Unique Binary Search Trees
题目:Given n, how many structurally unique BST's (binary search trees) that store values 1...n ?For example , Given n = 3, there are a total of 5 unique BST's. 1 3 3 2原创 2015-10-18 22:53:26 · 253 阅读 · 0 评论 -
Unique Binary Search Trees II
题目:Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.For example,Given n = 3, your program should return all 5 unique BST's shown below. 1原创 2015-10-19 07:56:46 · 355 阅读 · 0 评论 -
Pascal's Triangle II
题目:Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].思路:递推思路很明确,一层一层递推,并且只需要设立一个数组,本题不难。只需要注意一个解题公式即可代码:class Solution1 {public://f原创 2015-10-03 07:35:17 · 351 阅读 · 0 评论 -
Reverse Integer
题目:Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321思路:迭代计算,保存末尾值。注意一开始的负号第一个需要注意的就是判断最大值最小值,尤其是最小值,这一点在后面的求解除法中非常明显。代码:class Solution {publi原创 2015-10-03 07:42:42 · 307 阅读 · 0 评论 -
Minimum Depth of Binary Tree
题目:Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.思路:深搜+广搜深搜的方法和之前一样。主要讲一下广搜的原创 2015-10-20 19:51:22 · 398 阅读 · 0 评论 -
Multiply Strings
题目:Given two numbers represented as strings, return multiplication of the numbers as a string.思路:重新编写乘法、加法函数尤其注意究竟是数字操作还是字符本身操作,在好几个地方被卡主,都是保存为string的时候没有能够保存正确。代码:class Solution {pub原创 2015-10-02 09:33:41 · 390 阅读 · 0 评论 -
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.For example, given candidate set 2,3,6,7 and target 7,原创 2015-10-06 00:04:01 · 374 阅读 · 0 评论 -
Maximum Depth of Binary Tree
题目:Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.思路:深搜如果节点为空,返回0,;如果不为空,要看是否原创 2015-10-20 19:52:32 · 317 阅读 · 0 评论 -
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], [1,原创 2015-10-03 11:28:39 · 357 阅读 · 0 评论 -
Palindrome Partitioning II
题目:Given a string s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning of s.For example, given s = "aab",Ret原创 2015-10-21 22:07:33 · 302 阅读 · 0 评论 -
Minimum Path Sum
题目:Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or rig原创 2015-10-21 22:02:21 · 317 阅读 · 0 评论 -
Palindrome Partitioning
题目:Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s = "aab",Return [ ["aa","b原创 2015-10-21 22:06:20 · 353 阅读 · 0 评论 -
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:Elements in a triplet (原创 2015-10-21 22:09:31 · 324 阅读 · 0 评论 -
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 [2,原创 2015-10-23 07:28:50 · 333 阅读 · 0 评论 -
Search in Rotated Sorted Array II
题目:Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed ? Would this affect the run-time complexity? How and why?Write a function to determine if a given target is i原创 2015-10-23 07:43:01 · 342 阅读 · 0 评论 -
Longest Common Prefix
题目:Write a function to find the longest common prefix string amongst an array of strings.思路:最长公共前缀长度至少不会超过第一个数组的长度。并且竖向扫描,只要某一行的某一位不等于第一行对应的符号,就退出,否则就说明程序执行到最后。代码:class Solution {publ原创 2015-09-05 16:55:47 · 500 阅读 · 0 评论 -
Edit Distance
题目:Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have the following 3 operations permitted o原创 2015-09-18 22:30:53 · 369 阅读 · 0 评论 -
Letter Combinations of a Phone Number
题目: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.原创 2015-10-08 20:34:46 · 358 阅读 · 0 评论 -
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].思路:使用回原创 2015-10-23 07:28:03 · 361 阅读 · 0 评论 -
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: 每一行递增;每一行第一个数字比前一行最后一个数字大。 [1, 3, 5, 7], [10, 11, 16, 20], [23,原创 2015-09-21 11:29:57 · 482 阅读 · 0 评论 -
Search Insert Position
题目: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 arr原创 2015-10-25 09:08:27 · 331 阅读 · 0 评论 -
3Sum Closest
题目: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 ex原创 2015-10-25 09:10:26 · 322 阅读 · 0 评论 -
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 ]]原创 2015-10-25 09:15:19 · 319 阅读 · 0 评论 -
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, 4 ],原创 2015-10-25 09:20:54 · 420 阅读 · 0 评论 -
N-Queens
题目:The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to the n-queens puz原创 2015-10-07 07:43:52 · 525 阅读 · 0 评论 -
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: "((()))", "(()())", "(())()", "()(())",原创 2015-10-05 23:55:40 · 330 阅读 · 0 评论 -
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原创 2015-10-07 07:38:36 · 319 阅读 · 0 评论 -
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 the combinat原创 2015-10-07 00:38:46 · 408 阅读 · 0 评论 -
Swap Nodes in Pairs
题目:Given a linked list, swap every two adjacent nodes and return its head.Given 1->2->3->4, you should return the list as 2->1->4->3.思路:设立tail作为下一个list前一个结点,所以每次判断当前和之后是否为空 看了一下思路,立即ACCEP原创 2015-09-26 20:31:13 · 340 阅读 · 0 评论 -
Insert Interval
题目给出一些非重叠区间,插入一个新区间有[1,3],[6,9], 插入 [2,5] 结果为 [1,5],[6,9].有[1,2],[3,5],[6,7],[8,10],[12,16], 插入 [4,9] 结果为 [1,2],[3,10],[12,16].思路:大致三种情况主要注意区间范围的限制。代码class Solution原创 2015-10-01 20:39:33 · 375 阅读 · 0 评论 -
Sum Root to Leaf Numbers
题目:Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. 1 / \ 2 3Return the sum = 12 + 13 = 25.思路:递归首先有好几种思路,本程序写的是从顶端到本结点的一个原创 2015-09-26 19:42:13 · 313 阅读 · 0 评论 -
Plus One
题目:Given a non-negative number represented as an array of digits, plus one to the number.string1=“1213”,加1之后string=“1214”.思路: 转化+细节实现。代码:class Solution {public: vector plusOne(vect原创 2015-10-01 22:57:07 · 341 阅读 · 0 评论 -
Search for a Range
题目: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).For example,Given [5, 7, 7原创 2015-09-27 06:54:49 · 378 阅读 · 0 评论
分享