摘要: 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) 编辑
摘要: 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[... 阅读全文
posted @ 2014-02-18 08:16 Razer.Lu 阅读(301) 评论(0) 推荐(0) 编辑
摘要: Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?两个指针,一个一次走两步,一个一次走一步,相遇说明有环/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * ... 阅读全文
posted @ 2014-02-18 03:11 Razer.Lu 阅读(191) 评论(0) 推荐(0) 编辑
摘要: Clone an undirected graph. Each node in the graph contains alabeland a list of itsneighbors.OJ's undirected graph serialization:Nodes are labeled uniquely.We use#as a separator for each node, and,as a separator for node label and each neighbor of the node.As an example, consider the serialized g 阅读全文
posted @ 2014-02-18 02:43 Razer.Lu 阅读(278) 评论(0) 推荐(0) 编辑