随笔分类 - LeetCode
摘要:http://www.cnblogs.com/shawnhue/archive/2013/06/05/leetcode_126.htmlhttp://fisherlei.blogspot.com/2013/02/leetcode-word-ladder-ii-solution.htmlhttp://blog.csdn.net/niaokedaoren/article/details/8884938 1 class Node { 2 int no; 3 String val; 4 LinkedList prev; 5 6 Node(int no,...
阅读全文
摘要:TreeBinary Tree Preorder TraversalBinary Tree Inorder TraversalBinary Tree Postorder TraversalBinary Tree Level Order TraversalBinary Tree Level Order Traversal IIBinary Tree Zigzag Level Order TraversalMaximum Depth of Binary TreeMinimum Depth of Binary TreePath SumPath Sum IIBinary Tree Maximum Pa
阅读全文
摘要:Given an integern, generate a square matrix filled with elements from 1 ton2in spiral order.For example,Givenn=3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]生成的过程为 (1) 横向生成 1,2 (2) 纵向生成 3,4 (3) 横向生成 5,6 (4) 纵向生成 7,8 (5) 生成 9public class ...
阅读全文
摘要:Given a stringSand a stringT, count the number of distinct subsequences ofTinS.A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie,"ACE"
阅读全文
摘要:Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Ref:http://fisherlei.blogspot.com/20...
阅读全文
摘要:Given an array of integers, every element appearsthreetimes except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?Ref :http://fisherlei.blogspot.com/2013/11/leetcode-single-number-ii-solution.html[Thoughts]
阅读全文
摘要:Given an absolute path for a file (Unix-style), simplify it.For example,path="/home/", =>"/home"path="/a/./b/../../c/", =>"/c"click to show corner cases.Corner Cases:Did you consider the case wherepath="/../"?In this case, you should return&quo
阅读全文
摘要:Evaluate the value of an arithmetic expression inReverse Polish Notation.Valid operators are+,-,*,/. Each operand may be an integer or another express...
阅读全文
摘要:Givennpoints on a 2D plane, find the maximum number of points that lie on the same straight line.思路: 从 points[] 中依次取出一个点,然后依次和后面一个点组成一条直线。 y = ax+b;然后...
阅读全文
摘要:Sort a linked list inO(nlogn) time using constant space complexity.Merge sort先根据mid node把list 分割为 单个 node ,然后merge/** * Definition for singly-linked l...
阅读全文
摘要:Sort a linked list using insertion sort.思路: 建立一个fake head, 依次从list中取出一个node,插入到 fake head 的list中去,从小到大排列。public class Solution { public ListNode insertionSortList(ListNode head) { ListNode prehead = new ListNode(Integer.MIN_VALUE); ListNode runner = head; while(runner != null...
阅读全文
摘要:Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations:getandset.get(key)- Get the valu...
阅读全文
摘要:Given a singly linked listL:L0→L1→…→Ln-1→Ln,reorder it to:L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For exam...
阅读全文
摘要:Given a stringsand a dictionary of wordsdict, add spaces insto construct a sentence where each word is a valid dictionary word.Return all such possible sentences.For example, givens="catsanddog",dict=["cat", "cats", "and", "sand", "dog"].A
阅读全文
摘要:Given a stringsand a dictionary of wordsdict, determine ifscan be segmented into a space-separated sequence of one or more dictionary words.For example, givens="leetcode",dict=["leet", "code"].Return true because"leetcode"can be segmented as"leet code&quo
阅读全文
摘要:A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.Ref:http://fisherlei.blogspot.com/2013/11/leetcode-copy-list-with-random-pointer.html如图分三步:http://lh6.ggpht.com/-xlfWkNgeNhI/Uomwl3lB47I/A
阅读全文
摘要:Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.Follow up:Can you solve it without using extra space?解题思路:双指针, 一个指针一次走一步,一个指针一次走两步, 如果有环,定相遇。然后将快指针移到头部,再一次走一步,当两只真再次相遇的时候就是指针的头/** * Definition for singly-linked list. * class ListNode { * int val; * ...
阅读全文
摘要:Given an array of integers, every element appearstwiceexcept for one. Find that single one.Could you implement it without using extra memory?Ref:http://www.cnblogs.com/feiling/p/3349654.html[解题思路]以前看书时遇到过,一个数组中有一个元素出现一次,其他每个元素都出现两次要求空间复杂度为O(1)由于两个相同的元素异或的结果为0,而0^a==a,将数组中所有元素都进行异或,得到结果就是只出现一次的元素publ
阅读全文
摘要:There areNchildren standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requi...
阅读全文
摘要:There areNgas stations along a circular route, where the amount of gas at stationiisgas[i].You have a car with an unlimited gas tank and it costscost[...
阅读全文

浙公网安备 33010602011771号