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题解
:Sqrt(x)
Sqrt(x)Implementintsqrt(intx).Computeandreturnthesquarerootofx.思路:可以考虑二分搜索,但是二分搜索的起点不好判定,不过可以预先计算好numeric_limits::max()的平方根,然后搜索,这样范围就缩小很多而且避免了溢出。有的人用long来计算,这样在很多情况下也是可以避免溢出的。但是必须注意的是C++标准中long的最大值可以
MagiSu
·
2013-11-29 05:00
LeetCode
LeetCode题解
:Divide Two Integers
DivideTwoIntegersDividetwointegerswithoutusingmultiplication,divisionandmodoperator.思路:核心思想是:令试除数等于除数,如果试除数小于被除数,那么试除数乘二,商乘二,继续比较;否则试除数除二,商除二,被除数减去试除数,重新开始。这其实就是类似二分搜索法。题目有一些边边角角的特殊情况,比如int的最大值比最小值的绝对
MagiSu
·
2013-11-29 04:00
LeetCode
LeetCode题解
:Evaluate Reverse Polish Notation
EvaluateReversePolishNotationEvaluatethevalueofanarithmeticexpressioninReversePolishNotation.Validoperatorsare+,-,*,/.Eachoperandmaybeanintegeroranotherexpression.Someexamples:["2","1","+","3","*"]->(
MagiSu
·
2013-11-28 03:00
LeetCode
LeetCode题解
:Search in Rotated Sorted Array
SearchinRotatedSortedArraySupposeasortedarrayisrotatedatsomepivotunknowntoyoubeforehand.(i.e.,0124567mightbecome4567012).Youaregivenatargetvaluetosearch.Iffoundinthearrayreturnitsindex,otherwisereturn
MagiSu
·
2013-11-26 07:00
LeetCode
LeetCode题解
: Search for a Range
SearchforaRangeGivenasortedarrayofintegers,findthestartingandendingpositionofagiventargetvalue.Youralgorithm'sruntimecomplexitymustbeintheorderofO(logn).Ifthetargetisnotfoundinthearray,return[-1,-1].F
MagiSu
·
2013-11-26 06:00
LeetCode
LeetCode题解
:Surrounded Regions
SurroundedRegionsTotalAccepted:1947TotalSubmissions:13017Givena2Dboardcontaining'X'and'O',captureallregionssurroundedby'X'.Aregioniscapturedbyflippingall'O'sinto'X'sinthatsurroundedregion.Forexample,X
MagiSu
·
2013-11-26 04:00
LeetCode
LeetCode题解
: Reverse Linked List II
ReverseLinkedListIIReversealinkedlistfrompositionmton.Doitin-placeandinone-pass.Forexample:Given1->2->3->4->5->NULL,m=2andn=4,return1->4->3->2->5->NULL.Note:Givenm,nsatisfythefollowingcondition:1≤m≤n≤
MagiSu
·
2013-11-26 03:00
LeetCode
LeetCode题解
: Construct Binary Tree from Preorder and Inorder Traversal
ConstructBinaryTreefromPreorderandInorderTraversalGivenpreorderandinordertraversalofatree,constructthebinarytree.Note:Youmayassumethatduplicatesdonotexistinthetree.思路:和inorder+postorder的思路类似。题解:/** *D
MagiSu
·
2013-11-25 15:00
LeetCode
LeetCode题解
: Construct Binary Tree from Inorder and Postorder Traversal
ConstructBinaryTreefromInorderandPostorderTraversalGiveninorderandpostordertraversalofatree,constructthebinarytree.Note:Youmayassumethatduplicatesdonotexistinthetree.思路:已知中序遍历和后序遍历的结果,要求反推树结构。首先注意到后序遍
MagiSu
·
2013-11-25 14:00
LeetCode
LeetCode题解
:Decode Ways
DecodeWaysAmessagecontaininglettersfromA-Zisbeingencodedtonumbersusingthefollowingmapping:'A'->1 'B'->2 ... 'Z'->26 Givenanencodedmessagecontainingdigits,determinethetotalnumberofwaystodecodeit.Forexa
MagiSu
·
2013-11-25 13:00
LeetCode
LeetCode题解
:Combinations
CombinationsGiventwointegersnandk,returnallpossiblecombinationsofknumbersoutof1...n.Forexample,Ifn=4andk=2,asolutionis:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] 思路:递归填数即可。题解:classSolution{ public:
MagiSu
·
2013-11-25 03:00
LeetCode
LeetCode题解
:3sum closest
3SumClosestGivenanarraySofnintegers,findthreeintegersinSsuchthatthesumisclosesttoagivennumber,target.Returnthesumofthethreeintegers.Youmayassumethateachinputwouldhaveexactlyonesolution.Forexample,give
MagiSu
·
2013-11-25 00:00
LeetCode
LeetCode题解
:Combination Sum I and II
CombinationSumGivenasetofcandidatenumbers(C)andatargetnumber(T),findalluniquecombinationsinCwherethecandidatenumberssumstoT.ThesamerepeatednumbermaybechosenfromCunlimitednumberoftimes.Note:Allnumbers(
MagiSu
·
2013-11-25 00:00
LeetCode
LeetCode题解
:3Sum
3SumGivenanarraySofnintegers,arethereelementsa,b,cinSsuchthata+b+c=0?Findalluniquetripletsinthearraywhichgivesthesumofzero.Note:Elementsinatriplet(a,b,c)mustbeinnon-descendingorder.(ie,a≤b≤c)Thesoluti
MagiSu
·
2013-11-24 15:00
LeetCode
LeetCode题解
:Merge/Insert Interval
MergeIntervalsGivenacollectionofintervals,mergealloverlappingintervals.Forexample,Given[1,3],[2,6],[8,10],[15,18],return[1,6],[8,10],[15,18].InsertIntervalGivenasetofnon-overlappingintervals,insertane
MagiSu
·
2013-11-24 06:00
LeetCode
LeetCode题解
: Reverse Nodes in k-Group
ReverseNodesink-GroupGivenalinkedlist,reversethenodesofalinkedlistkatatimeandreturnitsmodifiedlist.Ifthenumberofnodesisnotamultipleofkthenleft-outnodesintheendshouldremainasitis.Youmaynotalterthevalue
MagiSu
·
2013-11-22 15:00
LeetCode
LeetCode题解
: Regular Expression Matching
RegularExpressionMatchingImplementregularexpressionmatchingwithsupportfor'.'and'*'.'.'Matchesanysinglecharacter. '*'Matcheszeroormoreoftheprecedingelement. Thematchingshouldcovertheentireinputstring(
MagiSu
·
2013-11-22 08:00
LeetCode
LeetCode题解
:String to Integer (atoi)
StringtoInteger(atoi)Implementatoitoconvertastringtoaninteger.Hint:Carefullyconsiderallpossibleinputcases.Ifyouwantachallenge,pleasedonotseebelowandaskyourselfwhatarethepossibleinputcases.Notes:Itisin
MagiSu
·
2013-11-19 05:00
LeetCode
LeetCode题解
:ZigZag Conversion
ZigZagConversionThestring"PAYPALISHIRING"iswritteninazigzagpatternonagivennumberofrowslikethis:(youmaywanttodisplaythispatterninafixedfontforbetterlegibility)PAHN APLSIIG YIR Andthenreadlinebyline:"PA
MagiSu
·
2013-11-19 05:00
LeetCode
LeetCode题解
:Longest Palindromic Substring
LongestPalindromicSubstringGivenastringS,findthelongestpalindromicsubstringinS.YoumayassumethatthemaximumlengthofSis1000,andthereexistsoneuniquelongestpalindromicsubstring.思路:只想到一个O(n^2)的解法。就是从左到右扫描起点
MagiSu
·
2013-11-19 04:00
LeetCode
LeetCode题解
:Sort List
SortListSortalinkedlistinO(nlogn)timeusingconstantspacecomplexity.思路:其实可以做一个快速排序或者merge排序之类的。但是这里的linkedlist是单向的,所以普通的快排办不到。不过仍然可以借用快速排序的思路,每次把链表分为三个部分:小于pivot,等于pivot和大于pivot,然后对小于大于pivot的部分进行排序。最后把三
MagiSu
·
2013-11-19 02:00
LeetCode
LeetCode题解
: Longest Consecutive Sequence
LongestConsecutiveSequenceGivenanunsortedarrayofintegers,findthelengthofthelongestconsecutiveelementssequence.Forexample,Given[100,4,200,1,3,2],Thelongestconsecutiveelementssequenceis[1,2,3,4].Returni
MagiSu
·
2013-11-17 09:00
LeetCode
LeetCode题解
: Flatten Binary Tree to Linked List
FlattenBinaryTreetoLinkedListGivenabinarytree,flattenittoalinkedlistin-place.Forexample,Given1 /\ 25 /\\ 346 Theflattenedtreeshouldlooklike:1 \ 2 \ 3 \ 4 \ 5 \ 6 思路:按preorder顺序访问树,使用迭代的方式进行。这样迭代过程中下一个
MagiSu
·
2013-11-17 07:00
LeetCode
LeetCode题解
: Binary Tree Zigzag Level Order Traversal
BinaryTreeZigzagLevelOrderTraversalGivenabinarytree,returnthezigzaglevelordertraversalofitsnodes'values.(ie,fromlefttoright,thenrighttoleftforthenextlevelandalternatebetween).Forexample:Givenbinarytre
MagiSu
·
2013-11-17 06:00
LeetCode
LeetCode题解
:Longest Valid Parentheses
LongestValidParenthesesGivenastringcontainingjustthecharacters'('and')',findthelengthofthelongestvalid(well-formed)parenthesessubstring.For"(()",thelongestvalidparenthesessubstringis"()",whichhaslengt
MagiSu
·
2013-11-17 00:00
LeetCode
LeetCode题解
:Set Matrix Zeroes
SetMatrixZeroesGivenamxnmatrix,ifanelementis0,setitsentirerowandcolumnto0.Doitinplace.思路:如果允许额外存储行和列清零的信息,那么题目就没有意思了。希望只用O(1)的存储空间的时候,我们就得利用矩阵本身的存储空间了:把清零的信息保存在第一行和第一列里。题解:classSolution{ public: voids
MagiSu
·
2013-11-16 12:00
LeetCode
LeetCode题解
:Word Break I and II
WordBreakGivenastringsandadictionaryofwordsdict,determineifscanbesegmentedintoaspace-separatedsequenceofoneormoredictionarywords.Forexample,givens="leetcode",dict=["leet","code"].Returntruebecause"lee
MagiSu
·
2013-11-15 05:00
LeetCode
LeetCode题解
:Rotate List
Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NU
·
2013-11-14 20:00
LeetCode
LeetCode题解
:Rotate List
RotateListGivenalist,rotatethelisttotherightbykplaces,wherekisnon-negative.Forexample:Given1->2->3->4->5->NULLandk=2,return4->5->1->2->3->NULL.思路:这个题目的陷阱中,输入为空指针大约不算其中之一。主要的问题是k可以大于这个链表的总长度。这样,真正的旋转次数
MagiSu
·
2013-11-14 05:00
LeetCode
LeetCode题解
:Insertion Sort List
InsertionSortListSortalinkedlistusinginsertionsort.思路:标准的插入排序。就是考察一下链表的操作而已。题解:/** *Definitionforsingly-linkedlist. *structListNode{ *intval; *ListNode*next; *ListNode(intx):val(x),next(NULL){} *}; */
MagiSu
·
2013-11-14 01:00
LeetCode
LeetCode题解
:Add Two Numbers
AddTwoNumbersYouaregiventwolinkedlistsrepresentingtwonon-negativenumbers.Thedigitsarestoredinreverseorderandeachoftheirnodescontainasingledigit.Addthetwonumbersandreturnitasalinkedlist.Input:(2->4->3)
MagiSu
·
2013-11-13 13:00
LeetCode
LeetCode题解
:Add Binary
AddBinaryGiventwobinarystrings,returntheirsum(alsoabinarystring).Forexample,a="11"b="1"Return"100".思路:小学编程竞赛题。题解:classSolution{ public: stringaddBinary(stringa,stringb){ boolcarry=false; constintLenA
MagiSu
·
2013-11-13 13:00
LeetCode
LeetCode题解
:Longest Substring Without Repeating Characters
LongestSubstringWithoutRepeatingCharactersGivenastring,findthelengthofthelongestsubstringwithoutrepeatingcharacters.Forexample,thelongestsubstringwithoutrepeatinglettersfor"abcabcbb"is"abc",whichthele
MagiSu
·
2013-11-13 13:00
LeetCode
LeetCode题解
:Longest Common Prefix
LongestCommonPrefixWriteafunctiontofindthelongestcommonprefixstringamongstanarrayofstrings.思路:两个字符串的LCP可以通过依次比较相应字符的方法来完成。题解:classSolution{ public: stringlcp(conststring&s1,conststring&s2) { constintL
MagiSu
·
2013-11-13 12:00
LeetCode
LeetCode题解
: Validate Binary Search Tree
ValidateBinarySearchTreeGivenabinarytree,determineifitisavalidbinarysearchtree(BST).AssumeaBSTisdefinedasfollows:Theleftsubtreeofanodecontainsonlynodeswithkeyslessthanthenode'skey.Therightsubtreeofano
MagiSu
·
2013-11-13 12:00
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
上一页
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
其他