
LeetCode
_IanXiao
这个作者很懒,什么都没留下…
展开
-
LeetCode204. Count Primes
题目链接: https://leetcode.com/problems/count-primes/题目描述:找出n以内的素数个数。题目分析:题目下面那个提示写的太好了,值得mark啊。埃拉托色尼筛选法,针对自然数列中的自然数而实施的,用于求一定范围内的质数。一个合数总是可以分解成若干个质数的乘积,那么如果把质数的倍数都去掉,那么剩下的就是质数了。 任意合数肯定都有一个因子小于合数的开方。原创 2016-01-28 20:11:05 · 390 阅读 · 0 评论 -
LeetCode131. 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"], ["a原创 2016-02-11 20:42:30 · 544 阅读 · 0 评论 -
LeetCode8. String to Integer (atoi)
题目链接: https://leetcode.com/problems/string-to-integer-atoi/题目描述:将字符串转化为int型整数。题目分析:题不难,细节太多。需要注意的是:在未处理数字字符前,数字前有空格的跳过,例如s=” 123” result=123;字符串中有‘+’,‘-’号,需要表示int型结果的正负,例如s=”-123” result=123;字符串原创 2016-01-13 12:02:43 · 417 阅读 · 0 评论 -
LeetCode67. Add Binary
题目链接: https://leetcode.com/problems/add-binary/题目描述:将二进制字符串相加。题目分析:嗯,需要注意的就是C++中数字转换字符串的方法。string numToStr(int i){ stringstream ss; ss<<i; return ss.str();}代码:class Solutio原创 2016-01-13 14:15:06 · 446 阅读 · 0 评论 -
LeetCode14. Longest Common Prefix
题目链接: https://leetcode.com/problems/longest-common-prefix/题目描述:找出所有字符串的最长公共前缀。题目分析:两个字符串的最长公共前缀,肯定不能超过其中短的字符串的长度。比较两个字符串1,2后找到了一个最长公共前缀长度为n,此时可以把前面的字符串2长度看成为n,再将字符串2与字符串3比较,再次找到一个新的最长公共前缀长度n_new,可以知原创 2016-01-13 18:19:06 · 446 阅读 · 0 评论 -
LeetCode53. Maximum Subarray
题目链接: https://leetcode.com/problems/maximum-subarray/题目描述:求最大连续子序列和。题目分析:参考 http://www.cnblogs.com/bakari/p/4007368.html用动态规划的方法,就是要找到其转移方程式,也叫动态规划的递推式,动态规划的解法无非是维护两个变量,局部最优和全局最优。local[i]表示以nums[原创 2016-01-31 14:38:29 · 356 阅读 · 0 评论 -
LeetCode152. Maximum Product Subarray
题目链接: https://leetcode.com/problems/maximum-product-subarray/题目描述:求最大连续子序列乘积。For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6.题目分析:还是动态规划的思想。维护局部最优原创 2016-01-31 14:50:11 · 385 阅读 · 0 评论 -
LeetCode221. Maximal Square
题目链接: https://leetcode.com/problems/maximal-square/题目描述:给出一个M*N的矩阵, 只有’1’, ‘0’,两种元素; 需要你从中找出 由’1’组成的最大正方形。题目分析:嗯,还是动态规划。换个角度看,就是找边长最大的正方形。 从前往后推。正方形左上,上边,左边都已确定,正方形右下的边长由左边,上边,左上控制。 以当前点(x,y) = ‘1原创 2016-02-01 12:30:04 · 453 阅读 · 0 评论 -
LeetCode274. H-Index
题目链接: https://leetcode.com/problems/h-index/题目描述:给出一个数组记录一个研究者各篇文章的引用数,写一个函数计算这个研究者的H指数。H指数是一个2005年由Jorge E. Hirsch提出的用于评估研究人员的学术产出数量与学术产出水平的指标。实现H指数的计算方法可以从它的定义入手,即一个人在其所有学术文章中有N篇论文分别被引用了至少N次,他的H指数原创 2016-02-01 13:29:02 · 725 阅读 · 0 评论 -
LeetCode1. Two Sum
题目链接: https://leetcode.com/problems/two-sum/题目描述:在一个数组(无序)中快速找出两个数字,使得两个数字之和等于一个给定的值。假设数组中肯定存在至少一组满足要求。题目分析:我做的是排序加双指针,这个方法不是很好,因为排序会改变对应下标。 参考了这篇博客 http://www.cnblogs.com/ganganloveu/p/3728849.h原创 2016-01-29 17:46:20 · 481 阅读 · 0 评论 -
LeetCode之双指针(3)
11. Container With Most Water#####题目链接: https://leetcode.com/problems/container-with-most-water/题目描述:给一个高度数组,代表x轴上有一些给定高度的竖线,求两条竖线与x轴构成的容器能容纳最大多少的水面积。题目分析:curCapacity=min(height[low],height[high])*(原创 2016-01-08 10:32:46 · 575 阅读 · 0 评论 -
LeetCode6. ZigZag Conversion
题目链接: https://leetcode.com/problems/zigzag-conversion/题目描述将字符串,循环对角化,在输出。比如: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20循环对角化成之字型。0 8 16 1原创 2016-01-12 21:54:59 · 517 阅读 · 0 评论 -
LeetCode36. Valid Sudoku
题目链接: https://leetcode.com/problems/valid-sudoku/题目描述:判断一个数独是不是合法。 每一行每一列每一宫的数字不能重复。题目分析:~~(╯﹏╰)b,就是判断每一宫的数字不能重复稍微麻烦了点。代码:class Solution {public: bool isValidSudoku(vector<vector<char>>& board原创 2016-01-28 20:19:53 · 382 阅读 · 0 评论 -
LeetCode77. Combinations
题目链接: https://leetcode.com/problems/combinations/题目描述:输出1~n的 k 个数字的所有排列组合。For example, If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]思路:回溯的基本题吧,感觉都是一个套路原创 2016-02-05 21:55:54 · 425 阅读 · 0 评论 -
LeetCode90. Subsets II
题目链接: https://leetcode.com/problems/subsets-ii/题目描述:给一个包含重复元素的数组,返回元素所有的可能的集合。 注意: 集合中的元素是非递减顺序; 解集中不包含相同组合的集合。思路:唯一的难点就是不包含相同组合的集合。跟LeetCode40. Combination Sum II差不多,用pre变量记录前一个元素,检查当前元素和前一个元素是否原创 2016-02-05 22:01:24 · 477 阅读 · 0 评论 -
LeetCode322. Coin Change
题目链接: https://leetcode.com/problems/coin-change/题目描述:给你n种钱币,然后给你一个总数amount,这些钱币可以用无数次,问你用这些钱币组成这个amount,最少的数量是多少?如果不能组成返回-1。 Example 1: coins = [1, 2, 5], amount = 11 return 3 (11 = 5 + 5 + 1)思路:原创 2016-02-05 22:09:33 · 493 阅读 · 0 评论 -
LeetCode279. Perfect Squares
题目链接: https://leetcode.com/problems/perfect-squares/题目描述:完美平方数,给定任意数n,它可表示为多个平方数(如1,4,9,16…)的和,求出表示出任意数n所需的平方数的最少个数。题目分析:动态规划思想。 找n需要的最少平方数个数,n=a+b*b,此时就是找a需要的最少平方数个数,n需要的最少平方数个数=a需要的最少平方数个数+1。代码:c原创 2016-01-28 20:36:06 · 364 阅读 · 0 评论 -
LeetCode60. Permutation Sequence
题目链接: https://leetcode.com/problems/permutation-sequence/题目描述:给一个集合[1,2….n]元素1~n,对它们进行从小到大顺序排列,找出第k个排列结果。思路:做不来啊,找不到规律。看了http://blog.youkuaiyun.com/doc_sgl/article/details/12840715 才有点感觉。 这道题其实是康托逆展开。{1原创 2016-02-06 20:27:17 · 699 阅读 · 0 评论 -
LeetCode47. Permutations II
题目链接: https://leetcode.com/problems/permutations-ii/题目描述:全排列问题。但是数组中可能包含重复元素,所以要避免解集包含重复组合的情况。思路:~~(╯﹏╰)b唯一需要注意的就是如何去掉重复元素带来的影响。如果当前元素和前一个的元素相等,并且前一个元素被选过了,就continue。代码:class Solution {public:原创 2016-02-06 20:35:20 · 416 阅读 · 0 评论 -
LeetCode64. Minimum Path Sum
题目链接: https://leetcode.com/problems/minimum-path-sum/题目描述:求矩阵从左上角到右下角最小路径和。题目分析:~~(╯﹏╰)b动态规划基础题。代码:class Solution {public: int minPathSum(vector<vector<int>>& grid) { int m=grid.size();原创 2016-01-29 16:37:53 · 421 阅读 · 0 评论 -
LeetCode165. Compare Version Numbers
题目链接: https://leetcode.com/problems/compare-version-numbers/题目描述:给出版本字符串,比较版本。Here is an example of version numbers ordering: 0.1 < 1.1 < 1.2 < 13.37题目分析:在遇到点之前的字符串如果转换成int型能比较出大小直接返回,如果相等,那么比较下一个点原创 2016-01-14 18:54:20 · 568 阅读 · 0 评论 -
LeetCode198. House Robber
题目链接: https://leetcode.com/problems/house-robber/题目描述:题目设计了一个抢劫犯的情景,其实就是求数组中不相邻数据进行组合得到的最大值。思路:dp[i]为以nums[i]为结尾的序列中不相邻数据组合得到的最大值。 当前位置i选不选由i-2,和i-1位置决定。 dp[i]=max(dp[i-2]+nums[i],dp[i-1]);代码:clas原创 2016-02-02 23:52:07 · 432 阅读 · 0 评论 -
LeetCode213. House Robber II
题目链接: https://leetcode.com/problems/house-robber-ii/题目描述:这道题就是在上一题(198. House Robber)的基础上加了一个条件,变成了环,所以如果抢了第一家,就不能抢最后一家。思路:我们可以分别求从第一家到倒数第二家的不相邻数据组合的最大值,从第二家到最后一家的不相邻数据组合的最大值。比较找出最终最大值。代码:class Solu原创 2016-02-02 23:56:49 · 443 阅读 · 0 评论 -
LeetCode109. Convert Sorted List to Binary Search Tree
题目: https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.思路:题意:给一个排好序的原创 2016-03-30 19:47:24 · 413 阅读 · 0 评论 -
LeetCode130. Surrounded Regions
题目: https://leetcode.com/problems/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 surrou原创 2016-03-31 18:02:25 · 494 阅读 · 0 评论 -
LeetCode337. House Robber III
题目: https://leetcode.com/problems/house-robber-iii/The 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原创 2016-03-23 20:34:40 · 1792 阅读 · 0 评论 -
LeetCode332. Reconstruct Itinerary
题目: https://leetcode.com/problems/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原创 2016-03-14 18:35:10 · 756 阅读 · 0 评论 -
LeetCode207. Course Schedule
题目: https://leetcode.com/problems/course-schedule/There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have prerequisites, for example to take course 0 you have原创 2016-03-27 20:50:55 · 413 阅读 · 0 评论 -
从数组中选出n个数之和为k
LeetCode15. 3Sum题目描述: https://leetcode.com/problems/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 th原创 2016-04-11 20:17:56 · 7761 阅读 · 0 评论 -
求最长等差数列长度
Longest Consecutive Sequence https://leetcode.com/problems/longest-consecutive-sequence/LeetCode上面这道题是求最长连续子序列。换种说法就是求公差为1的最长等差数列。题目描述Given an unsorted array of integers, find the length of the long原创 2016-05-14 09:46:05 · 3828 阅读 · 1 评论 -
LeetCode300. Longest Increasing Subsequence
题目链接: https://leetcode.com/problems/longest-increasing-subsequence/题目描述:求最长增长子序列包含数字个数。Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101], therefore the length原创 2016-02-02 12:48:57 · 399 阅读 · 0 评论 -
LeetCode134. Gas Station
题目:There are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its原创 2016-02-20 23:01:53 · 431 阅读 · 0 评论 -
LeetCode236 Lowest Common Ancestor of a Binary Tree
题目链接: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/题目描述:给一棵树,找两结点的最近公共祖先。3 / \ 5 1 / \ / \ 6 2 0 8 / \ 7 4LCA of nodes 5 and 1 is 3LCA of nodes 5 and 4 is 5题目分析:原创 2016-01-01 12:08:23 · 333 阅读 · 0 评论 -
LeetCode123. Best Time to Buy and Sell Stock III
题目链接: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/题目描述:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格。最多交易两次,手上最多只能持有一支股票,求最大收益。思路:最多交易两次。可以以i天为分界线,一个记录以prices[i]为结尾的最大收益(即i天之前的最大收益,从前往后),一原创 2016-02-04 20:24:49 · 466 阅读 · 0 评论 -
LeetCode78. Subsets
题目链接: https://leetcode.com/problems/subsets/题目描述:给一个数组,把它当做一个集合,求这个集合的所有子集。子集元素降序排列。For example,If nums = [1,2,3], a solution is:[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], []]思原创 2016-02-03 00:14:36 · 738 阅读 · 0 评论 -
LeetCode55. Jump Game
题目:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if you原创 2016-02-16 21:23:23 · 380 阅读 · 0 评论 -
LeetCode45. Jump Game II
题目:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal is to r原创 2016-02-16 21:29:48 · 380 阅读 · 0 评论 -
LeetCode39. Combination Sum
题目链接: https://leetcode.com/problems/combination-sum/题目描述:给出一组候选数字(C)和目标数字(T),找出C中所有的组合,使组合中数字的和为T。C中每个数字在每个组合中可多次使用。组合中的数字降序排列。For example, given candidate set 2,3,6,7 and target 7, A solution set原创 2016-02-03 10:41:05 · 467 阅读 · 0 评论 -
LeetCode40. Combination Sum II
题目链接: https://leetcode.com/problems/combination-sum-ii/题目描述:给出一组候选数字(C)和目标数字(T),找出C中所有的组合,使组合中数字的和为T。C中每个数字在每个组合中只能使用一次。跟39.Combination Sum差不多。注意解集不能包含重复的组合,比如[1,1] 1 解集只能是[[1]],不能是[[1],[1]]思路:排序原创 2016-02-03 10:49:11 · 345 阅读 · 0 评论 -
LeetCode216. Combination Sum III
题目链接: https://leetcode.com/problems/combination-sum-iii/题目描述:寻找所有满足k个数之和等于n的组合,只允许使用数字1-9,并且每一种组合中的数字应该是唯一的。确保组合中的数字以递增顺序排列。思路:回溯。水题。代码:class Solution {public: vector<vector<int>> res; vect原创 2016-02-04 20:06:50 · 435 阅读 · 0 评论