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题解
:Sort Colors
SortColorsGivenanarraywithnobjectscoloredred,whiteorblue,sortthemsothatobjectsofthesamecolorareadjacent,withthecolorsintheorderred,whiteandblue.Here,wewillusetheintegers0,1,and2torepresentthecolorred,
MagiSu
·
2013-11-13 12:00
LeetCode
LeetCode题解
:N-Queens I and II
N-QueensThen-queenspuzzleistheproblemofplacingnqueensonann×nchessboardsuchthatnotwoqueensattackeachother.Givenanintegern,returnalldistinctsolutionstothen-queenspuzzle.Eachsolutioncontainsadistinctboar
MagiSu
·
2013-11-13 11:00
LeetCode
LeetCode题解
:Palindrome Number
PalindromeNumberDeterminewhetheranintegerisapalindrome.Dothiswithoutextraspace.思路:题目要求只能用O(1)的空间,所以不能考虑把它转化为字符串然后reverse比较的方法。在提示中也提到了,如果考虑reversenumber的方法,可能造成溢出。那么只能选择分离数字的方法,当然要用到大量除10和取余数的计算。真是一件悲
MagiSu
·
2013-11-13 11:00
LeetCode
LeetCode题解
:Remove Nth Node from End of List
RemoveNthNodeFromEndofListGivenalinkedlist,removethenthnodefromtheendoflistandreturnitshead.Forexample,Givenlinkedlist:1->2->3->4->5,andn=2. Afterremovingthesecondnodefromtheend,thelinkedlistbecomes1
MagiSu
·
2013-11-10 10:00
LeetCode
LeetCode题解
:Minimum Path Sum
MinimumPathSumGivenamxngridfilledwithnon-negativenumbers,findapathfromtoplefttobottomrightwhichminimizesthesumofallnumbersalongitspath.Note:Youcanonlymoveeitherdownorrightatanypointintime.思路:一个简单的动态规划
MagiSu
·
2013-11-10 06:00
LeetCode
LeetCode题解
:Gray Code
GrayCodeThegraycodeisabinarynumeralsystemwheretwosuccessivevaluesdifferinonlyonebit.Givenanon-negativeintegernrepresentingthetotalnumberofbitsinthecode,printthesequenceofgraycode.Agraycodesequencemust
MagiSu
·
2013-11-10 05:00
LeetCode
LeetCode题解
:Convert Sorted Array to Binary Search Tree
ConvertSortedArraytoBinarySearchTreeGivenanarraywhereelementsaresortedinascendingorder,convertittoaheightbalancedBST.思路:给定一个区间[left,right],取其中值mid=(left+right)/2对应的元素作为二叉树的根。二叉树的左子树根的值为对[left,mid-1]继续
MagiSu
·
2013-11-10 05:00
LeetCode
【
LeetCode题解
】Remove Element
RemoveElement (Java代码)MySubmissionsGivenanarrayandavalue,removeallinstancesofthatvalueinplaceandreturnthenewlength.Theorderofelementscanbechanged.Itdoesn'tmatterwhatyouleavebeyondthenewlength.publiccl
cc1044588189
·
2013-11-09 22:00
java
LeetCode
element
remove
【
LeetCode题解
】Search Insert Position
SearchInsertPosition (Java代码)Givenasortedarrayandatargetvalue,returntheindexifthetargetisfound.Ifnot,returntheindexwhereitwouldbeifitwereinsertedinorder.Youmayassumenoduplicatesinthearray.Herearefewex
cc1044588189
·
2013-11-09 21:00
java
LeetCode
search
insert
Positi
【
LeetCode题解
】Single Number II
SingleNumberII (Java代码)Givenanarrayofintegers,everyelementappears three timesexceptforone.Findthatsingleone.Note:Youralgorithmshouldhavealinearruntimecomplexity.Couldyouimplementitwithoutusingextramem
cc1044588189
·
2013-11-08 17:00
java
位运算
number
single
II
LeetCode题解
:Partition List
PartitionListGivenalinkedlistandavaluex,partitionitsuchthatallnodeslessthanxcomebeforenodesgreaterthanorequaltox.Youshouldpreservetheoriginalrelativeorderofthenodesineachofthetwopartitions.Forexample,
MagiSu
·
2013-11-07 04:00
LeetCode
【
LeetCode题解
】Reverse Integer
ReverseInteger(Java代码)Reversedigitsofaninteger.Example1: x=123,return321Example2: x=-123,return-321clicktoshowspoilers.Haveyouthoughtaboutthis?Herearesomegoodquestionstoaskbeforecoding.Bonuspointsfory
cc1044588189
·
2013-11-06 00:00
java
LeetCode
Integer
reverse
【
LeetCode题解
】Single Number
SingleNumber(Java代码)Givenanarrayofintegers,everyelementappears twice exceptforone.Findthatsingleone.Note:Youralgorithmshouldhavealinearruntimecomplexity.Couldyouimplementitwithoutusingextramemory?解题思路
cc1044588189
·
2013-11-06 00:00
java
LeetCode
number
single
LeetCode题解
:Symmetric Tree
SymmetricTreeGivenabinarytree,checkwhetheritisamirrorofitself(ie,symmetricarounditscenter).Forexample,thisbinarytreeissymmetric:1 /\ 22 /\/\ 3443 Butthefollowingisnot:1 /\ 22 \\ 33 Note:Bonuspointsify
MagiSu
·
2013-11-05 16:00
LeetCode
LeetCode题解
:Climbing Stairs
ClimbingStairsYouareclimbingastaircase.Ittakesnstepstoreachtothetop.Eachtimeyoucaneitherclimb1or2steps.Inhowmanydistinctwayscanyouclimbtothetop?思路:这种题目属于那种“想到解法就很快做出”的类型。n个台阶的楼梯的走法等于:ways(n)=ways(n-1)
MagiSu
·
2013-11-05 15:00
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
上一页
25
26
27
28
29
30
31
32
下一页
按字母分类:
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
其他