上一页 1 2 3 4 5 6 ··· 16 下一页
摘要: Sort a linked list inO(nlogn) time using constant space complexity.Merge sort先根据mid node把list 分割为 单个 node ,然后merge/** * Definition for singly-linked l... 阅读全文
posted @ 2014-02-20 10:19 Razer.Lu 阅读(264) 评论(0) 推荐(0) 编辑
摘要: 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... 阅读全文
posted @ 2014-02-19 15:47 Razer.Lu 阅读(156) 评论(0) 推荐(0) 编辑
摘要: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations:getandset.get(key)- Get the valu... 阅读全文
posted @ 2014-02-19 09:57 Razer.Lu 阅读(261) 评论(0) 推荐(0) 编辑
摘要: 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... 阅读全文
posted @ 2014-02-19 09:01 Razer.Lu 阅读(138) 评论(0) 推荐(0) 编辑
摘要: 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 阅读全文
posted @ 2014-02-19 08:12 Razer.Lu 阅读(167) 评论(0) 推荐(0) 编辑
摘要: 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 阅读全文
posted @ 2014-02-19 02:18 Razer.Lu 阅读(333) 评论(0) 推荐(0) 编辑
摘要: 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 阅读全文
posted @ 2014-02-18 14:39 Razer.Lu 阅读(252) 评论(0) 推荐(0) 编辑
摘要: 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; * ... 阅读全文
posted @ 2014-02-18 13:55 Razer.Lu 阅读(138) 评论(0) 推荐(0) 编辑
摘要: 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 阅读全文
posted @ 2014-02-18 08:41 Razer.Lu 阅读(174) 评论(0) 推荐(0) 编辑
摘要: 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... 阅读全文
posted @ 2014-02-18 08:33 Razer.Lu 阅读(158) 评论(0) 推荐(0) 编辑
上一页 1 2 3 4 5 6 ··· 16 下一页