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题解
:Search Insert Position
SearchInsertPositionGivenasortedarrayandatargetvalue,returntheindexifthetargetisfound.Ifnot,returntheindexwhereitwouldbeifitwereinsertedinorder.Youmayassumenoduplicatesinthearray.Herearefewexamples.[1
MagiSu
·
2013-11-05 15:00
LeetCode
LeetCode题解
:Populating Next Right Pointers in Each Node I and II
PopulatingNextRightPointersinEachNodeGivenabinarytreestructTreeLinkNode{ TreeLinkNode*left; TreeLinkNode*right; TreeLinkNode*next; } Populateeachnextpointertopointtoitsnextrightnode.Ifthereisnonextrig
MagiSu
·
2013-11-05 15:00
LeetCode
LeetCode题解
:Remove Element
RemoveElementGivenanarrayandavalue,removeallinstancesofthatvalueinplaceandreturnthenewlength.Theorderofelementscanbechanged.Itdoesn'tmatterwhatyouleavebeyondthenewlength.思路:左右两个指针依次指向数组开始和结束。左右指针移动,将所
MagiSu
·
2013-11-05 14:00
LeetCode
LeetCode题解
:Same Tree
SameTreeGiventwobinarytrees,writeafunctiontocheckiftheyareequalornot.Twobinarytreesareconsideredequaliftheyarestructurallyidenticalandthenodeshavethesamevalue.思路:递归比较即可。题解:/** *Definitionforbinarytree
MagiSu
·
2013-11-05 10:00
LeetCode
LeetCode题解
:Linked List Cycle
LinkedListCycleGivenalinkedlist,determineifithasacycleinit.Followup:Canyousolveitwithoutusingextraspace?思路:参见ElementsofProgramming,其中讨论orbit的时候提到了这个问题的方法。也就是选择两个指针,一个指针跑得比另外一个指针快。如果跑得快的指针遇到了终点,那么就不存在环
MagiSu
·
2013-11-05 09:00
LeetCode
LeetCode题解
:Integer to Roman
IntegertoRomanGivenaninteger,convertittoaromannumeral.Inputisguaranteedtobewithintherangefrom1to3999.思路:没有思路。题解:classSolution{ public: stringintToRoman(intnum){ staticconststringSYMBOLS="MDCLXVI"; st
MagiSu
·
2013-11-05 09:00
LeetCode
LeetCode题解
:Reorder List
ReorderListGivenasinglylinkedlistL:L0→L1→…→Ln-1→Ln,reorderitto:L0→Ln→L1→Ln-1→L2→Ln-2→…Youmustdothisin-placewithoutalteringthenodes'values.Forexample,Given{1,2,3,4},reorderitto{1,4,2,3}.思路:因为链表是单向的,只能通
MagiSu
·
2013-11-05 09:00
LeetCode
LeetCode题解
:Multiply Strings
MultiplyStringsGiventwonumbersrepresentedasstrings,returnmultiplicationofthenumbersasastring.Note:Thenumberscanbearbitrarilylargeandarenon-negative.思路:两件事情:1.字符串表示的数字和单个字符表示的数字的乘法2.两个字符串表示的数字的求和。这些都是初
MagiSu
·
2013-11-05 09:00
LeetCode
LeetCode题解
:Remove Duplicates from Sorted Array I and II
RemoveDuplicatesfromSortedArrayIIFollowupfor"RemoveDuplicates":Whatifduplicatesareallowedatmosttwice?Forexample,GivensortedarrayA=[1,1,1,2,2,3],Yourfunctionshouldreturnlength=5,andAisnow[1,1,2,2,3].Re
MagiSu
·
2013-11-05 06:00
LeetCode
LeetCode题解
:Clone Graph
CloneGraphCloneanundirectedgraph.Eachnodeinthegraphcontainsalabelandalistofitsneighbors.思路:首先对这个图进行搜索,深度优先广度优先是任意的。每碰到一个之前未遇到的结点则创造一个相应的新结点与这个结点对照,同时新结点的所有邻居仍然和原始结点的邻居相同。然后更新新结点的邻居信息,在之前的搜索中新旧结点的对应关系是
MagiSu
·
2013-11-05 05:00
LeetCode
LeetCode题解
:Spiral Matrix I and II
SpiralMatrixGivenamatrixofmxnelements(mrows,ncolumns),returnallelementsofthematrixinspiralorder.Forexample,Giventhefollowingmatrix:[ [1,2,3], [4,5,6], [7,8,9] ] Youshouldreturn[1,2,3,6,9,8,7,4,5].Spir
MagiSu
·
2013-11-05 04:00
LeetCode
LeetCode题解
:Search a 2D Matrix
Searcha2DMatrixWriteanefficientalgorithmthatsearchesforavalueinanmxnmatrix.Thismatrixhasthefollowingproperties:Integersineachrowaresortedfromlefttoright.Thefirstintegerofeachrowisgreaterthanthelastint
MagiSu
·
2013-11-05 04:00
LeetCode
LeetCode题解
:Rotate Image
RotateImageYouaregivenannxn2Dmatrixrepresentinganimage.Rotatetheimageby90degrees(clockwise).Followup:Couldyoudothisin-place?思路:可以参见ProgrammingPearls,很多时候旋转是可以用reverse代替的。做顺时针90度旋转,可以先转置矩阵,然后对矩阵的每一行进行r
MagiSu
·
2013-11-05 03:00
LeetCode
LeetCode题解
:Triangle
TriangleGivenatriangle,findtheminimumpathsumfromtoptobottom.Eachstepyoumaymovetoadjacentnumbersontherowbelow.Forexample,giventhefollowingtriangle[ [2], [3,4], [6,5,7], [4,1,8,3] ] Theminimumpathsumfro
MagiSu
·
2013-11-05 01:00
LeetCode
LeetCode题解
:Minimum Depth of Binary Tree
MinimumDepthofBinaryTreeGivenabinarytree,finditsminimumdepth.Theminimumdepthisthenumberofnodesalongtheshortestpathfromtherootnodedowntothenearestleafnode.思路:首先要区分两种结点:非leaf的结点和leaf的结点。leaf结点的左右子树均为空。然
MagiSu
·
2013-11-05 01:00
LeetCode
LeetCode题解
:Binary Tree Level Order Traversal I and II
BinaryTreeLevelOrderTraversalGivenabinarytree,returnthelevelordertraversalofitsnodes'values.(ie,fromlefttoright,levelbylevel).Forexample:Givenbinarytree{3,9,20,#,#,15,7},3 /\ 920 /\ 157 returnitslevel
MagiSu
·
2013-11-04 11:00
LeetCode
LeetCode题解
:Permutations I and II
PermutationsGivenacollectionofnumbers,returnallpossiblepermutations.Forexample,[1,2,3]havethefollowingpermutations:[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],and[3,2,1].PermutationsIIGivenacollectionofnu
MagiSu
·
2013-11-04 02:00
LeetCode
LeetCode题解
:Interleaving String
InterleavingStringGivens1,s2,s3,findwhethers3isformedbytheinterleavingofs1ands2.Forexample,Given:s1="aabcc",s2="dbbca",Whens3="aadbbcbcac",returntrue.Whens3="aadbbbaccc",returnfalse.思路:其实是一个动态规划问题:设o1
MagiSu
·
2013-11-04 02:00
LeetCode
LeetCode题解
:Next Permutation
NextPermutationImplementnextpermutation,whichrearrangesnumbersintothelexicographicallynextgreaterpermutationofnumbers.Ifsucharrangementisnotpossible,itmustrearrangeitasthelowestpossibleorder(ie,sorted
MagiSu
·
2013-11-04 00:00
LeetCode
LeetCode题解
:Minimum Window Substring
MinimumWindowSubstringGivenastringSandastringT,findtheminimumwindowinSwhichwillcontainallthecharactersinTincomplexityO(n).Forexample,S="ADOBECODEBANC"T="ABC"Minimumwindowis"BANC".Note:Ifthereisnosuchw
MagiSu
·
2013-11-03 23:00
LeetCode
LeetCode题解
:Roman to Integer
RomantoIntegerGivenaromannumeral,convertittoaninteger.Inputisguaranteedtobewithintherangefrom1to3999.思路:字母对数字的一一对应累积关系。唯一需要注意的是,之前一个字母对应的数字如果小于当前字母对应的数字,则需要特殊处理。题解:std::mapchar_mapper{ {'I',1}, {'V',5
MagiSu
·
2013-11-02 06:00
LeetCode
LeetCode题解
:Simplify Path
SimplifyPathGivenanabsolutepathforafile(Unix-style),simplifyit.Forexample,path="/home/",=>"/home"path="/a/./b/../../c/",=>"/c"思路:其实就是一个依次处理以'/'为分割的数据的工具。如果碰到空输入或者'.'就什么都不做,如果碰到".."就从队列里退出一个元素。最后将队列里的元
MagiSu
·
2013-11-02 06:00
LeetCode
LeetCode题解
:Remove Duplicates from Sorted List II
RemoveDuplicatesfromSortedListIIGivenasortedlinkedlist,deleteallnodesthathaveduplicatenumbers,leavingonlydistinctnumbersfromtheoriginallist.Forexample,Given1->2->3->3->4->4->5,return1->2->5.Given1->1-
MagiSu
·
2013-11-02 05:00
LeetCode
LeetCode题解
:Remove Duplicates from Sorted List
RemoveDuplicatesfromSortedListGivenasortedlinkedlist,deleteallduplicatessuchthateachelementappearonlyonce.Forexample,Given1->1->2,return1->2.Given1->1->2->3->3,return1->2->3.思路:用两个指针。一个指针不断前进,另一个指针不动。
MagiSu
·
2013-11-02 02:00
LeetCode
LeetCode题解
:Letter Combinations of a Phone Number
LetterCombinationsofaPhoneNumberGivenadigitstring,returnallpossiblelettercombinationsthatthenumbercouldrepresent.Amappingofdigittoletters(justlikeonthetelephonebuttons)isgivenbelow.Input:Digitstring"2
MagiSu
·
2013-10-31 03:00
LeetCode
LeetCode题解
:Pascal's triangle I and II
Pascal'sTriangleGivennumRows,generatethefirstnumRowsofPascal'striangle.Forexample,givennumRows=5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]Pascal'sTriangleIIGivenanindexk,returnthekthrowoft
MagiSu
·
2013-10-31 03:00
LeetCode
LeetCode题解
:Reverse integer
ReverseIntegerReversedigitsofaninteger.Example1:x=123,return321Example2:x=-123,return-321思路:不断用对10取余的方法分离数字,然后反过来即可。注意符号位。题解:classSolution{ public: intreverse(intx){ intflag=(x>=0?1:-1); x*=flag; intr
MagiSu
·
2013-10-27 13:00
LeetCode
LeetCode题解
:Single Number I and II
SingleNumberGivenanarrayofintegers,everyelementappearstwiceexceptforone.Findthatsingleone.Note:Youralgorithmshouldhavealinearruntimecomplexity.Couldyouimplementitwithoutusingextramemory?SingleNumberII
MagiSu
·
2013-10-27 13:00
LeetCode
LeetCode题解
:Maximum Depth of Binary Tree
MaximumDepthofBinaryTreeGivenabinarytree,finditsmaximumdepth.Themaximumdepthisthenumberofnodesalongthelongestpathfromtherootnodedowntothefarthestleafnode.思路:树其实是图的一种特例。在对树进行搜索的时候保留树深的信息。一旦遇到深度比已知最大深度大
MagiSu
·
2013-10-27 13:00
LeetCode
LeetCode题解
:Copy List with Random Pointer
CopyListwithRandomPointerAlinkedlistisgivensuchthateachnodecontainsanadditionalrandompointerwhichcouldpointtoanynodeinthelistornull.Returnadeepcopyofthelist.思路:比起标准的链表深拷贝,还需要了解一个randompointer的信息。需要建立起
MagiSu
·
2013-10-27 13:00
LeetCode
LeetCode题解
:Valid Palindrome
ValidPalindromeGivenastring,determineifitisapalindrome,consideringonlyalphanumericcharactersandignoringcases.Forexample,"Aman,aplan,acanal:Panama"isapalindrome."raceacar"isnotapalindrome.Note:Haveyouc
MagiSu
·
2013-10-27 08:00
LeetCode
LeetCode题解
:Restore IP Addresses
RestoreIPAddressesGivenastringcontainingonlydigits,restoreitbyreturningallpossiblevalidIPaddresscombinations.Forexample:Given"25525511135",return["255.255.11.135","255.255.111.35"].(Orderdoesnotmatter
MagiSu
·
2013-10-27 04:00
LeetCode
LeetCode题解
:Unique Paths II
UniquePathsIIFollowupfor"UniquePaths":Nowconsiderifsomeobstaclesareaddedtothegrids.Howmanyuniquepathswouldtherebe?Anobstacleandemptyspaceismarkedas1and0respectivelyinthegrid.Forexample,Thereisoneobsta
MagiSu
·
2013-10-26 19:00
LeetCode
LeetCode题解
:Unique Paths
UniquePathsArobotislocatedatthetop-leftcornerofamxngrid(marked'Start'inthediagrambelow).Therobotcanonlymoveeitherdownorrightatanypointintime.Therobotistryingtoreachthebottom-rightcornerofthegrid(marke
MagiSu
·
2013-10-26 19:00
LeetCode
LeetCode题解
:Valid Sudoku
ValidSudokuDetermineifaSudokuisvalid,accordingto:SudokuPuzzles-TheRules.TheSudokuboardcouldbepartiallyfilled,whereemptycellsarefilledwiththecharacter'.'.Apartiallyfilledsudokuwhichisvalid.思路:遍历整个棋盘,记录
MagiSu
·
2013-10-26 15:00
LeetCode
LeetCode题解
:Anagrams
AnagramsGivenanarrayofstrings,returnallgroupsofstringsthatareanagrams.Note:Allinputswillbeinlower-case.思路:关键在于对题目的理解。本文中anagrams指只有字母排列顺序不同的单词,例如eat,ate,tea。倘若有多组anagrams,那么返回所有的anagrams。两个空字符串也算一对ana
MagiSu
·
2013-10-26 15:00
LeetCode
LeetCode题解
:Count and Say
CountandSayThecount-and-saysequenceisthesequenceofintegersbeginningasfollows:1,11,21,1211,111221,...1isreadoffas"one1"or11.11isreadoffas"two1s"or21.21isreadoffas"one2,thenone1"or1211.Givenanintegern,g
MagiSu
·
2013-10-26 13:00
LeetCode
LeetCode题解
:Subsets I and II
SubsetsGivenasetofdistinctintegers,S,returnallpossiblesubsets.Note:Elementsinasubsetmustbeinnon-descendingorder.Thesolutionsetmustnotcontainduplicatesubsets.Forexample,IfS=[1,2,3],asolutionis:[ [3], [
MagiSu
·
2013-10-24 04:00
LeetCode
LeetCode题解
:Merge Sorted Array
MergeSortedArrayGiventwosortedintegerarraysAandB,mergeBintoAasonesortedarray.Note:YoumayassumethatAhasenoughspacetoholdadditionalelementsfromB.ThenumberofelementsinitializedinAandBaremandnrespectively
MagiSu
·
2013-10-24 04:00
LeetCode
LeetCode题解
:Pow(x,n)
Pow(x,n)Implementpow(x,n).思路:1.直接循环求解,最简单的方法2.分而治之,可以减少一半的乘法。3.如果n可能是浮点数的话,要考虑用对数运算。题解:classSolution{ public: doublepow_proc(doublex,intn){ doublep; switch(n) { case0: return1; case1: returnx; default
MagiSu
·
2013-10-22 11:00
LeetCode
LeetCode题解
:Valid Parentheses
ValidParenthesesGivenastringcontainingjustthecharacters'(',')','{','}','['and']',determineiftheinputstringisvalid.Thebracketsmustcloseinthecorrectorder,"()"and"()[]{}"areallvalidbut"(]"and"([)]"arenot
MagiSu
·
2013-10-22 11:00
LeetCode
LeetCode题解
:Sum Root to Leaf Numbers
SumRoottoLeafNumbersGivenabinarytreecontainingdigitsfrom0-9only,eachroot-to-leafpathcouldrepresentanumber.Anexampleistheroot-to-leafpath1->2->3whichrepresentsthenumber123.Findthetotalsumofallroot-to-l
MagiSu
·
2013-10-22 11:00
LeetCode
LeetCode题解
:Jump Game
JumpGameGivenanarrayofnon-negativeintegers,youareinitiallypositionedatthefirstindexofthearray.Eachelementinthearrayrepresentsyourmaximumjumplengthatthatposition.Determineifyouareabletoreachthelastinde
MagiSu
·
2013-10-21 00:00
LeetCode
LeetCode题解
:Balanced Binary Tree
BalancedBinaryTreeGivenabinarytree,determineifitisheight-balanced.Forthisproblem,aheight-balancedbinarytreeisdefinedasabinarytreeinwhichthedepthofthetwosubtreesofeverynodeneverdifferbymorethan1.思路:二叉树
MagiSu
·
2013-10-20 07:00
LeetCode
LeetCode题解
:Merge k Sorted Lists
MergekSortedListsMergeksortedlinkedlistsandreturnitasonesortedlist.Analyzeanddescribeitscomplexity思路:标准的mergesort使用min(x,y),这里需要的是min(x1,x2,...xk),可以用堆实现。题解:/** *Definitionforsingly-linkedlist. *struc
MagiSu
·
2013-10-18 19:00
LeetCode题解
:Length of Last Word
LengthofLastWordGivenastringsconsistsofupper/lower-casealphabetsandemptyspacecharacters'',returnthelengthoflastwordinthestring.Ifthelastworddoesnotexist,return0.Note:Awordisdefinedasacharactersequence
MagiSu
·
2013-10-17 09:00
LeetCode
LeetCode题解
:Two Sum
TwoSumGivenanarrayofintegers,findtwonumberssuchthattheyadduptoaspecifictargetnumber.ThefunctiontwoSumshouldreturnindicesofthetwonumberssuchthattheyadduptothetarget,whereindex1mustbelessthanindex2.Plea
MagiSu
·
2013-10-16 05:00
LeetCode
上一页
24
25
26
27
28
29
30
31
下一页
按字母分类:
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
其他