[LeetCode] Find Peak Element 求数组的局部峰值

  Apeakelementisanelementthatisgreaterthanitsneighbors. Givenaninputarray nums,where nums[i]≠nums[i+1],findapeakelementandreturnitsindex. Thearraymaycontainmultiplepeaks,inthatcasereturntheindextoanyoneofthepeaksisfine. Youmayimagineth

[LeetCode] Random Pick Index 随机拾取序列

  Givenanarrayofintegerswithpossibleduplicates,randomlyoutputtheindexofagiventargetnumber.Youcanassumethatthegiventargetnumbermustexistinthearray. Note: Thearraysizecanbeverylarge.Solutionthatusestoomuchextraspacewillnotpassthejudge. &

[LeetCode] Minimum Height Trees 最小高度树

  Foraundirectedgraphwithtreecharacteristics,wecanchooseanynodeastheroot.Theresultgraphisthenarootedtree.Amongallpossiblerootedtrees,thosewithminimumheightarecalledminimumheighttrees(MHTs).Givensuchagraph,writeafunctiontofindalltheMHTsandreturnalisto

[LeetCode] Reconstruct Itinerary 重建行程单

  Givenalistofairlineticketsrepresentedbypairsofdepartureandarrivalairports [from,to],reconstructtheitineraryinorder.Alloftheticketsbelongtoamanwhodepartsfrom JFK.Thus,theitinerarymustbeginwith JFK. Note: Iftherearemultiplevaliditineraries,

[LeetCode] 210. Course Schedule II 课程清单之二

  Thereareatotalof n coursesyouhavetotake,labeledfrom 0 to n-1. Somecoursesmayhaveprerequisites,forexampletotakecourse0youhavetofirsttakecourse1,whichisexpressedasapair: [0,1] Giventhetotalnumberofcoursesandalistofprerequisite pairs,returnt

[LeetCode] 207. Course Schedule 课程清单

  Thereareatotalof n coursesyouhavetotake,labeledfrom 0 to n-1. Somecoursesmayhaveprerequisites,forexampletotakecourse0youhavetofirsttakecourse1,whichisexpressedasapair: [0,1] Giventhetotalnumberofcoursesandalistofprerequisite pairs,isitpos

LeetCode Binary Search Summary 二分搜索法小结

  二分查找法作为一种常见的查找方法,将原本是线性时间提升到了对数时间范围,大大缩短了搜索时间,具有很大的应用场景,而在LeetCode中,要运用二分搜索法来解的题目也有很多,但是实际上二分查找法的查找目标有很多种,而且在细节写法也有一些变化。之前有网友留言希望博主能针对二分查找法的具体写法做个总结,博主由于之前一直很忙,一直拖着没写,为了

[LeetCode] 141. Linked List Cycle 单链表中的环

  Givenalinkedlist,determineifithasacycleinit. Torepresentacycleinthegivenlinkedlist,weuseaninteger pos whichrepresentstheposition(0-indexed) inthelinkedlistwheretailconnectsto.If pos is -1,thenthereisnocycleinthelinkedlist.   Ex

[LeetCode] 743. Network Delay Time 网络延迟时间

  Thereare N networknodes,labelled 1 to N. Given times,alistoftraveltimesas directededges times[i]=(u,v,w),where u isthesourcenode, v isthetargetnode,and w isthetimeittakesforasignaltotravelfromsourcetotarget. Now,wesendasignalfromacertainn

Leetcode 887 Super Egg Drop(扔鸡蛋) DP

这是经典的扔鸡蛋的题目。同事说以前在uva上见过,不过是扔气球。题意如下: 题意: 你有K个鸡蛋,在一栋N层高的建筑上,被要求测试鸡蛋最少在哪一层正好被摔坏。 你只能用没摔坏的鸡蛋测试。如果一个鸡蛋在上一次测试中没有被摔坏,那么你可以重复使用,否则,你只能用下一个鸡蛋。 需要求,最小的步数,使得你在这么多步内一定测试出结果。 思路: O(K*N^2) 首先,这个

[LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树

  Giveninorderandpostordertraversalofatree,constructthebinarytree. Note: Youmayassumethatduplicatesdonotexistinthetree.   这道题要求从中序和后序遍历的结果来重建原二叉树,我们知道中序的遍历顺序是左-根-右,后序的顺序是左-右-根,对于这种树的重建一般都是采用递归来做,可参见我之前的一篇博客ConvertSortedArray

[LeetCode] Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树

  Givenpreorderandinordertraversalofatree,constructthebinarytree. Note:Youmayassumethatduplicatesdonotexistinthetree.   这道题要求用先序和中序遍历来建立二叉树,跟之前那道ConstructBinaryTreefromInorderandPostorderTraversal由中序和后序遍历建立二叉树原理基本相同,针对这道题,由于先序的顺序的第一个肯定

[LeetCode] Repeated Substring Pattern 重复子字符串模式

  Givenanon-emptystringcheckifitcanbeconstructedbytakingasubstringofitandappendingmultiplecopiesofthesubstringtogether.YoumayassumethegivenstringconsistsoflowercaseEnglishlettersonlyanditslengthwillnotexceed10000. E

[LeetCode] 70. Climbing Stairs 爬楼梯问题

  Youareclimbingastaircase.Ittakes n stepstoreachtothetop. Eachtimeyoucaneitherclimb1or2steps.Inhowmanydistinctwayscanyouclimbtothetop? Note: Given n willbeapositiveinteger. Example1: Input:2 Output:2 Explanation:Therear

[LeetCode] Construct Quad Tree 建立四叉树

  Wewanttousequadtreestostorean NxN booleangrid.Eachcellinthegridcanonlybetrueorfalse.Therootnoderepresentsthewholegrid.Foreachnode,itwillbesubdividedintofourchildrennodes untilthevaluesintheregionitrepresentsareallthesame. Eachnodehasanothertwo

[LeetCode] 426. Convert Binary Search Tree to Sorted Doubly Linked List 将二叉搜索树转为有序双向链表

  ConvertaBSTtoasortedcirculardoubly-linkedlistin-place.Thinkoftheleftandrightpointersassynonymoustothepreviousandnextpointersinadoubly-linkedlist. Let'stakethefollowingBSTasanexample,itmayhelpyouunderstandtheproblembetter:   &#1

[LeetCode] Serialize and Deserialize N-ary Tree N叉搜索树的序列化和去序列化

  Serializationistheprocessofconvertingadatastructureorobjectintoasequenceofbitssothatitcanbestoredinafileormemorybuffer,ortransmittedacrossanetworkconnectionlinktobereconstructedlaterinthesameoranothercomputerenvironment. Designanal

[LeetCode] N-ary Tree Level Order Traversal N叉树层序遍历

  Givenann-arytree,returnthelevelordertraversalofitsnodes'values.(ie,fromlefttoright,levelbylevel). Forexample,givena 3-ary tree:     Weshouldreturnitslevelordertraversal:     [ [1], [3,2,4], [5

[LeetCode] Encode N-ary Tree to Binary Tree 将N叉树编码为二叉树

  DesignanalgorithmtoencodeanN-arytreeintoabinarytreeanddecodethebinarytreetogettheoriginalN-arytree.AnN-arytreeisarootedtreeinwhicheachnodehasnomorethanNchildren.Similarly,abinarytreeisarootedtreeinwhicheachnodehasnomorethan2children.Thereisnorestri

[LeetCode] 430. Flatten a Multilevel Doubly Linked List 压平一个多层的双向链表

  Youaregivenadoublylinkedlistwhichinadditiontothenextandpreviouspointers,itcouldhaveachildpointer,whichmayormaynotpointtoaseparatedoublylinkedlist.Thesechildlistsmayhaveoneormorechildrenoftheirown,andsoon,toproduceamultileveldatastructure,asshownint