E-COM-NET
首页
在线工具
Layui镜像站
SUI文档
联系我们
推荐频道
Java
PHP
C++
C
C#
Python
Ruby
go语言
Scala
Servlet
Vue
MySQL
NoSQL
Redis
CSS
Oracle
SQL Server
DB2
HBase
Http
HTML5
Spring
Ajax
Jquery
JavaScript
Json
XML
NodeJs
mybatis
Hibernate
算法
设计模式
shell
数据结构
大数据
JS
消息中间件
正则表达式
Tomcat
SQL
Nginx
Shiro
Maven
Linux
Leetcode题解
Leetcode题解
(28)
90.SubsetsII题目分析:代码如下1classSolution{ 2public: 3vector>subsetsWithDup(vector&S){ 4vector>result; 5map,bool>m; 6intsize=S.size(); 7for(inti=0;icur; 11for(intj=size-1;j>=0;j--) 12{ 13if(!tag) 1
LC凑热闹
·
2016-02-03 10:00
Leetcode题解
(27)
86.PartitionList题目分析:题目要求将链表划分为两部分,前半部分小于x,后半部分大于等于x,并且各个数之间的相对顺序不变。解题思路是:从头开始扫描链表,找打第一个大于等于x的数current,然后从这个数开始,把current之后的小于x的数依次插在current前,大于等于x的数不变;为了实现插入操作,可以新建一个带头节点的链表。代码如下:1/** 2*Definitionfor
LC凑热闹
·
2016-02-02 16:00
Leetcode题解
(26)
80.RemoveDuplicatesfromSortedArrayII题目分析:简单的操作,代码如下:1classSolution{ 2public: 3intremoveDuplicates(vector&nums){ 4intn=nums.size(); 5if(0==n) 6return0; 7 8inti=0; 9inttemp; 10intres=n; 11vect
LC凑热闹
·
2016-02-02 15:00
Leetcode题解
(25)
77.Combinations题目分析:求给定数字n,k的组合数,方法是采用深度搜索算法,代码如下(copy网上代码)1classSolution{ 2public: 3voiddfs77(vector>&ans,vectorsubans,intstart,intn,intk) 4{ 5if(subans.size()==k) 6{ 7ans.push_back(subans);ret
LC凑热闹
·
2016-02-01 16:00
Leetcode题解
(24)
73.SetMatrixZeroes分析:如果没有空间限制,这道题就很简单,但是要求空间复杂度为O(1),因此需要一些技巧。代码如下(copy网上的代码)classSolution{ public: voidsetZeroes(vector>&matrix) { boolbColZero=false,bRowZero=false; if(matrix.size()==0||matri
LC凑热闹
·
2016-02-01 15:00
Leetcode题解
(23)
69.Sqrt(x)题目分析,题目实现求一个int数的平方根,最暴力的算法就是逐个遍历,从1开始到x,判断是否有一个数i,其中满足i*ix;这个算法发虽然简单,但是效率不高。其实,按照暴力算法的思想,我们可以联想到在一个已经排好序的数组中查找一个数,该数正好满足上面的条件,因此可以采用二分查找的思想。代码如下:1classSolution{ 2public: 3intmySqrt(intx){
LC凑热闹
·
2016-02-01 12:00
Leetcode题解
(22)
66.PlusOne题目这题很简单,直接代码:1classSolution{ 2public: 3vectorplusOne(vector&digits){ 4//IMPORTANT:Pleaseresetanymemberdatayoudeclared,as 5//thesameSolutioninstancewillbereusedforeachtestcase. 6inta=1;
LC凑热闹
·
2016-02-01 11:00
Leetcode题解
(21)
62.UniquePaths题目分析:机器人一共要走m+n-2步,现在举个例子类比,有一个m+n-2位的二进制数,现在要在其中的m位填0,其余各位填1,一共有C(m+n-2,m-1)种可能,如果0表示向下走,1表示向右走,这样就和题目意思一样了。现在考虑最后一步的走法,要么向右走到达终点,要么向下走到达终点,因此f(m,n)=f(m,n-1)+f(m-1,n);代码如下(主要考虑的是大数据): 1
LC凑热闹
·
2016-01-30 16:00
Leetcode题解
(20)
59.SpiralMatrixII题目这道题copy网上的代码1classSolution{ 2private: 3intstep[4][2]; 4boolcanUse[100][100]; 5public: 6voiddfs(intdep,vector>&matrix,intdirect,intx,inty) 7{ 8for(inti=0;i>generateMatrix(intn
LC凑热闹
·
2016-01-30 15:00
Leetcode题解
(十九)
54、SpiralMatrix题目:题目意思很简单,就是螺旋式访问矩阵元素。也没有比较经典的算法可以解决此题,只需要模拟一下这个过程即可。代码如下:1classSolution{ 2public: 3vectorspiralOrder(vector>&matrix){ 4vectorres; 5if(matrix.empty()) 6returnres; 7intm=matrix.si
LC凑热闹
·
2016-01-30 15:00
Leetcode题解
(十八)
51、N-Queens---------------------------------------------------------------------------------分割线------------------------------------------------------------------52、N-QueensII--------------------------
LC凑热闹
·
2016-01-30 13:00
LeetCode题解
--1. Two Sum(和为S的两个数字)
链接题目地址:https://leetcode.com/problems/two-sum/Github代码:https://github.com/gatieme/LeetCode/tree/master/001-TwoSumCSDN题解:http://blog.csdn.net/gatieme/article/details/50596965描述给定一个整数数组,找出其中两个数满足相加等于你指定的
JeanCheng
·
2016-01-27 22:25
┈┈【LeetCode
面试题】
LeetCode题解
--1-TwoSum
题目地址https://leetcode.com/problems/two-sum/描述给定一个整数数组,找出其中两个数满足相加等于你指定的目标数字。要求:这个函数twoSum必须要返回能够相加等于目标数字的两个数的索引,且index1必须要小于index2。请注意一点,你返回的结果(包括index1和index2)都不是基于0开始的,即自1开始你可以假设每一个输入肯定只有一个结果。举例:输入:n
gatieme
·
2016-01-27 22:00
LeetCode
map
hash
LeetCode题解
#3 Longest Substring Without Repeating Characters
找出字符串中没有相同字符的的最长串注意这里的 Characters指的是字符,不是字母,就是说|/?~这样的字符都会出现,所以要用到ASCII码 最简单的方法是,从第一个字符开始,往后一个个判断,里面有没有重复的字符,如果重复了则记录下长度。例如:abcabcbb第一次:abc重复于a 长度3第二次:bca重复与b 长度3第三次:cab 重复与c 长度3...... 但这种方法很耗时如果是abcd
wzben
·
2016-01-19 20:00
LeetCode题解
——Longest Consecutive Sequence
Givenanunsortedarrayofintegers,findthelengthofthelongestconsecutiveelementssequence.Forexample,Given [100,4,200,1,3,2],Thelongestconsecutiveelementssequenceis [1,2,3,4].Returnitslength: 4.Youralgorith
u010025211
·
2016-01-18 19:00
LeetCode
hash
LeetCode题解
——Create Maximum Number
Giventwoarraysoflength m and n withdigits 0-9 representingtwonumbers.Createthemaximumnumberoflength kk&&j>0&&ans[j-1]0.Whatiftheyareequalagain?Wecontinuetolookthenextdigituntiltheyarenotequal.Ifalldig
u010025211
·
2016-01-16 11:00
dp
greedy
LeetCode题解
——Power of Three
Givenaninteger,writeafunctiontodetermineifitisapowerofthree.Followup:Couldyoudoitwithoutusinganyloop/recursion?Credits:Specialthanksto @dietpepsi foraddingthisproblemandcreatingalltestcases.classSolut
u010025211
·
2016-01-08 18:00
LeetCode
Math
LeetCode题解
——Compare Version Numbers
Comparetwoversionnumbers version1 and version2.If version1 > version2 return1,if version1 num2)return1; num1=0; num2=0; i++; j++; } return0; } };
u010025211
·
2016-01-06 19:00
LeetCode
String
LeetCode题解
——wordBreak2
Givenastring s andadictionaryofwords dict,addspacesin s toconstructasentencewhereeachwordisavaliddictionaryword.Returnallsuchpossiblesentences.Forexample,givens = "catsanddog",dict = ["cat","cats","an
u010025211
·
2016-01-06 18:00
LeetCode
dp
backtracking
Leetcode题解
——Word Break
Givenastring s andadictionaryofwords dict,determineif s canbesegmentedintoaspace-separatedsequenceofoneormoredictionarywords.Forexample,givens = "leetcode",dict = ["leet","code"].Returntruebecause "le
u010025211
·
2016-01-06 17:00
LeetCode
dp
recursive
LeetCode题解
——Rotate Image
Youaregivenan n x n 2Dmatrixrepresentinganimage.Rotatetheimageby90degrees(clockwise).Followup:Couldyoudothisin-place?/* *clockwiserotate *firstreverseuptodown,thenswapthesymmetry *123789741 *456=>456=
u010025211
·
2015-12-16 22:00
LeetCode
LeetCode题解
——Substring with Concatenation of All Words
Youaregivenastring, s,andalistofwords, words,thatareallofthesamelength.Findallstartingindicesofsubstring(s)in s thatisaconcatenationofeachwordin wordsexactlyonceandwithoutanyinterveningcharacters.Fore
u010025211
·
2015-12-16 21:00
LeetCode
String
map
table
hash
LeetCode题解
——Remove Invalid Parentheses
Removetheminimumnumberofinvalidparenthesesinordertomaketheinputstringvalid.Returnallpossibleresults.Note:Theinputstringmaycontainlettersotherthantheparentheses ( and ).Examples:"()())()"->["()()()","(
u010025211
·
2015-12-14 22:00
LeetCode
backtracking
LeetCode题解
——Sudoku Solver
WriteaprogramtosolveaSudokupuzzlebyfillingtheemptycells.Emptycellsareindicatedbythecharacter '.'.Youmayassumethattherewillbeonlyoneuniquesolution.Asudokupuzzle...classSolution{ public: boolcol[10][10]
u010025211
·
2015-12-01 20:00
LeetCode
backtracking
LeetCode题解
——Evaluate Reverse Polish Notation
Evaluatethevalueofanarithmeticexpressionin ReversePolishNotation.Validoperatorsare +, -, *, /.Eachoperandmaybeanintegeroranotherexpression.Someexamples:["2","1","+","3","*"]->((2+1)*3)->9 ["4","13","5
u010025211
·
2015-11-27 15:00
LeetCode
stack
LeetCode题解
——First Missing Positive
Givenanunsortedintegerarray,findthefirstmissingpositiveinteger.Forexample,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Youralgorithmshouldrunin O(n)timeandusesconstantspace.解题思路:如果可以对数组排序就好解决了,但是排序的
u010025211
·
2015-11-22 19:00
LeetCode
array
sort
LeetCode题解
——Minimum Path Sum
Givena m x n gridfilledwithnon-negativenumbers,findapathfromtoplefttobottomrightwhich minimizes thesumofallnumbersalongitspath.Note: Youcanonlymoveeitherdownorrightatanypointintime.解题思路:动态规划,p[i][j]表示
u010025211
·
2015-11-18 20:00
LeetCode
dp
LeetCode题解
——Path Sum II
Givenabinarytreeandasum,findallroot-to-leafpathswhereeachpath'ssumequalsthegivensum.Forexample:Giventhebelowbinarytreeand sum=22,5 /\ 48 //\ 11134 /\/\ 7251 return[ [5,4,11,2], [5,8,4,5] ] 解题思路:在paths
u010025211
·
2015-11-18 19:00
LeetCode
SUM
Path
BinaryTree
LeetCode题解
——Path Sum
Givenabinarytreeandasum,determineifthetreehasaroot-to-leafpathsuchthataddingupallthevaluesalongthepathequalsthegivensum.Forexample:Giventhebelowbinarytreeand sum=22,5 /\ 48 //\ 11134 /\\ 721 returntru
u010025211
·
2015-11-17 21:00
LeetCode
SUM
Path
BinaryTree
LeetCode题解
——Sum Root to Leaf Numbers
Givenabinarytreecontainingdigitsfrom 0-9 only,eachroot-to-leafpathcouldrepresentanumber.Anexampleistheroot-to-leafpath 1->2->3 whichrepresentsthenumber 123.Findthetotalsumofallroot-to-leafnumbers.Fore
u010025211
·
2015-11-17 21:00
TO
root
SUM
num
Leaf
BinaryTree
LeetCode题解
——Reverse Integer
题目: 数字翻转,即输入123,返回321;输入-123,返回-321。 代码: 1 class Solution { 2 public: 3 int reverse(int x) { 4 int result = 0, sign = 1; 5 if(x < 0) //负数转换为正数统一处理 6 { 7
·
2015-11-11 15:12
LeetCode
LeetCode题解
——ZigZag Conversion
题目: 把一个字符串按照Z型排列后打印出来,例如 "PAYPALISHIRING" 重新排列后为3行,即 P A H N A P L S I I G Y I R 那么输出为"PAHNAPLSIIGYIR" 解法: 细节实现题,假如重新排列为5行,那么排列后的下标分布应该为 0 &n
·
2015-11-11 15:11
conversion
LeetCode题解
——Add Two Numbers
题目: 两个数字求和,数字用链表表示,每一个结点代表一位。链表顺序与数字顺序相反,即表头存放数字的最低位。 解法: 分别遍历两个链表的每个结点,对两个结点求和即可。要维护一个变量保存每次相加之后的进位。 更常见的,链表顺序与数字顺序相同,那么做一次链表逆序,求和之后再逆序回来即可。 代码: 1 /** 2 * Definition for sin
·
2015-11-11 15:10
LeetCode
LeetCode题解
——Longest Substring Without Repeating Characters
题目: 给定一个字符串,返回其中不包含重复字符的最长子串长度。 解法: 维持两个指针,第一个指向子串开始,第二个负责遍历,当遍历到的字符出现在子串内时,应计算当前子串长度,并更新最长值;然后第一个指针更新为出现位置的下一个。 代码: 1 class Solution { 2 public: 3 int lengthOfLongestSubs
·
2015-11-11 15:09
substring
LeetCode题解
——Longest Palindromic Substring
题目: 给定一个字符串S,返回S中最长的回文子串。S最长为1000,且最长回文子串是唯一。 解法: ①遍历,对于每个字符,计算以它为中心的回文子串长度(长度为奇数),同时计算以它和右边相邻字符为中心的回文子串长度(长度为偶数)。时间为O(N2)。 ②另外,有一个很奇妙的算法,称为Manacher算法,参考 http://www.cnblogs.com/daolua
·
2015-11-11 15:09
substring
LeetCode题解
——Median of Two Sorted Arrays
题目: 找两个排序数组A[m]和B[n]的中位数,时间复杂度为O(log(m+n))。 解法: 更泛化的,可以找第k个数,然后返回k=(m+n)/2时的值。 代码: 1 class Solution 2 { 3 public: 4 double findMedianSortedArrays(int A[], int m, int B[],
·
2015-11-11 15:08
LeetCode
LeetCode题解
——Two Sum
题目地址:https://oj.leetcode.com/problems/two-sum/ Two Sum Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return i
·
2015-11-11 15:07
LeetCode
LeetCode题解
——4Sum
题目: 给定一个数组,找出其中和为0的所有4个数组合,每个组合内的4个数非递降。 解法: ①先排序,然后利用4个指针,前两个遍历,后两个在第二个指针之后的部分里夹逼,时间O(N3)。 ②或者利用一个哈希表先保存每两个数的和,然后类似于查找两个数的和等于给定值的过程。 代码: ① 1 class Solution { 2 public: 3
·
2015-11-11 10:04
LeetCode
LeetCode题解
——3Sum Closest
题目: 给定一个数组,和一个指定的值,找出数组中3个数,它的和最接近这个指定值,并返回这个和。 解法: 和上一题找3个数的和为0一样,先排序再遍历,这一次不需要记录路径。 代码: 1 class Solution { 2 public: 3 int threeSumClosest(vector<int> &num, in
·
2015-11-11 10:03
LeetCode
LeetCode题解
——3Sum
题目: 给定一个数组,找出其中和为0的所有3个数的组合。每个组合的3个数都是非递降的。 解法: 先排序再遍历,设置3个指针,第一个依次遍历,第二三个在第一个指针后面的部分里,左右夹逼查找和为第一个数的相反数的组合。时间O(N2)。 代码: 1 class Solution { 2 public: 3 vector<vector<
·
2015-11-11 10:02
LeetCode
LeetCode题解
——Longest Common Prefix
题目: 给定一系列的字符串,找出这些字符串的最长公共前缀。 解法: 暴力法,依次比较每个字符串的每个字符,碰到第一个不同的就返回之前找到的前缀。 代码: 1 class Solution { 2 public: 3 string longestCommonPrefix(vector<string> &strs) { 4
·
2015-11-11 10:02
LeetCode
LeetCode题解
——Roman to Integer
题目: 将罗马数字转换为整数。 解法: 可以参考上一篇数字转换为罗马数字的规则。 代码: 1 class Solution { 2 public: 3 int sym2int(char sym) //罗马数字字符与数字的对应关系 4 { 5 switch(sym) 6 { 7 case
·
2015-11-11 10:01
LeetCode
LeetCode题解
——Integer to Roman
题目: 将整数转换为罗马数字。罗马数字规则可以参考: 维基百科-罗马数字 解法: 类似于进制转换,从大的基数开始,求整数对基数的商和余,来进行转换。 代码: 1 class Solution { 2 public: 3 string intToRoman(int num) { 4 string result; 5
·
2015-11-11 10:00
LeetCode
LeetCode题解
——Container With Most Water
题目: x轴上有一些点,每个点上有一条与x轴垂直的线(线的下端就是这个点,不超出x轴),给出每条线的高度,求这些线与x轴组成的最大面积。 解法: 贪心策略,维持两个指针,分别指向第一个和最后一个元素,对于其中小的一个,它所能围成的最大面积就是到另一个元素之间,所以小的一个要往中间走一步。 代码: 1 class Solution { 2 public
·
2015-11-11 10:59
LeetCode
LeetCode题解
——Palindrome Number
题目: 判断一个数字是不是回文数字,即最高位与最低位相同,次高位与次低位相同,... 解法: 求出数字的位数,然后依次求商和求余判断是否相等。 代码: 1 class Solution { 2 public: 3 bool isPalindrome(int x) { 4 if(x < 0) //负数有符号,肯定不是
·
2015-11-11 10:59
LeetCode
LeetCode题解
——Regular Expression Matching
题目: 正则表达式的匹配,'.'能匹配任何一个字符,'*'之前必须有一个字符,两个结合起来表示之前那个字符出现0到无穷次。 解法: 一定要注意'*'必须结合前面的字符一起使用。 代码: 1 class Solution { 2 public: 3 bool isMatch(const char *s, const char *p) { 4
·
2015-11-11 10:58
LeetCode题解
——String to Integer(atoi)
题目: 字符串转换为数字。 解法: 这道题的意思是要考虑到,如果有前置的空字符,则跳过;如果超出数字范围,则返回最大/最小整数;如果碰到第一个不能转换的字符,则返回。 代码: 1 class Solution { 2 public: 3 int atoi(const char *str) { 4 int sign = 1,
·
2015-11-11 10:57
LeetCode
LeetCode题解
——Unique Path(DP与优化)
题目: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
·
2015-11-11 00:52
LeetCode
LeetCode题解
——Balanced Binary Tree
Givenabinarytree,determineifitisheight-balanced.Forthisproblem,aheight-balancedbinarytreeisdefinedasabinarytreeinwhichthedepthofthetwosubtreesof every nodeneverdifferbymorethan1.递归判断左子树是否是平衡二叉树,递归判断右子
u010025211
·
2015-11-08 21:00
LeetCode
tree
binary
depth
LeetCode题解
——Flatten Binary Tree to Linked List
Givenabinarytree,flattenittoalinkedlistin-place.Forexample,Given1 /\ 25 /\\ 346 Theflattenedtreeshouldlooklike:1 \ 2 \ 3 \ 4 \ 5 \ 6Hints:Ifyounoticecarefullyintheflattenedtree,eachnode'srightchildpoi
u010025211
·
2015-11-08 21:00
LeetCode
tree
binary
flatten
上一页
22
23
24
25
26
27
28
29
下一页
按字母分类:
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
其他