又是一道快慢指针与链表的结合题 Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.

Note: Do not modify the linked list.

Follow up:
Can you solve it without using extra space?


引用一下别人的解答

my solution is like this: using two pointers, one of them one step at a time. another pointer each take two steps. Suppose the first meet at stepk,the length of the Cycle isr. so..2k-k=nr,k=nr
Now, the distance between the start node of list and the start node of cycle is s. the distance between the start of list and the first meeting node isk(the pointer which wake one step at a time waked k steps).the distance between the start node of cycle and the first meeting node ism, so...s=k-m,
s=nr-m=(n-1)r+(r-m),here we takes n = 1
..so, using one pointer start from the start node of list, another pointer start from the first meeting node, all of them wake one step at a time, the first time they meeting each other is the start of the cycle.

此外:http://bookshadow.com/weblog/2015/07/10/leetcode-linked-list-cycle-ii/ 这篇博客讲解的很详细

代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
      if(head==nullptr||head->next==nullptr)return nullptr;
      ListNode *fast,*slow;
      fast=head;
      slow=head;
      bool iscircle=false;
      while(slow&&fast)
      {
          slow=slow->next;
          if(!fast->next)return nullptr;
          fast=fast->next->next;
          if(fast==slow)
          {
              iscircle=true;
              break;
          }
          
      }
      fast=head;
      if(iscircle==true)
      {
          while(fast!=slow)
          {fast=fast->next;
          slow=slow->next;}
          return slow;
      }
       return nullptr;
      
          }
};

此题目的一道变形:

找到重复的数字(lint code Find the Duplicate Number

Given an array nums containing n + 1integers where each integer is between 1and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.

Given nums = [5,4,3,2,1,5] return 5
Given nums = [5,4,4,3,2,1] return 4

这里给出一种新颖的方法:映射找环法,其实这种方法来源于链表找环。时间复杂度为O(n),空间复杂度O(1)

首先,假设一个数组中没有重复,则可以将数组的下标和其对应元素(1 ~ n)一一的映射起来。举个例子,对于数组[4, 1, 3, 2],则映射关系为0 -> 4, 1 -> 1, 2 -> 3,3 -> 2

这个映射关系可表示一个函数f(n),其中n是下标,f(n)是映射到的数。如果我们从下标为0出发,根据这个函数计算出一个值,以这个值为新的下标,再用这个函数计算,以此类推,可以产生一个类似于链表的序列。

此时,如果数组本身有重复的元素,就会出现多对一的映射,比如数组[2, 1, 3, 1],则映射关系为:0 -> 2, {1, 3} -> 1, 2 -> 3。熟悉链表的朋友知道,这一序列中出现了环路!与链表一致,如果遍历这个映射关系:0 -> 2 -> 3 -> 1 -> 1 -> 1 -> ...,其中,环的起点就是题目所要求的重复的数。

所以该题实际上可转化为求环路起点,实现方法与Linked List Cycle II一题类似。同样,使用快慢(fastslow)两个下标,从0开始,快下标每次循环时映射两次,即:fast = num[num[fast]],而慢下标每次循环映射一次,即:slow = num[slow],直到fast == slow,这个阶段相当于链表找环时,快指针与慢指针相遇,这一结论证明链表有环,对应到本题中,则能证明数组内有重复元素!

第二步,是找出这个重复元素,也就是找出环的入口,这时需要另一个变量finder从下标0出发,和fast的操作一致,每次循环映射两次:fast = num[num[fast]],同时,slow保持一次映射。当这两个下标相遇时,slow就是环的起点,也就是重复的数。

代码:

class Solution {
public:
    /**
     * @param nums an array containing n + 1 integers which is between 1 and n
     * @return the duplicate one
     */
    int findDuplicate(vector<int>& nums) {
        // Write your code here
       
      int slow=0;
      int fast=0;
      while(1)
      {
          slow=nums[slow];
          fast=nums[nums[fast]];
          if(slow==fast)
          break;
      }
      int find=0;
      while(find!=slow)
      {
          find=nums[find];
          slow=nums[slow];
      }
      return slow;
      
     
        
    }
};

此外这道题还可以用二分法来做

思路是采用了二分法+抽屉原理。首先解释一下为什么用二分法,因为O(n2)时间复杂度不能A,所以往下应该是n*logn,很容易联想到二分法,因为其复杂度为logn。

抽屉原理是说假设你有11个苹果,要放进10个抽屉,那么至少有一个抽屉里是有两个苹果的。

对应到这题,1~n的n+1个数字,有1个数字会至少重复两次。

比如取数组为{1,2,2,3,4,5},一共6个数,范围是1~5,其中位数应该是(5+1)/2 = 3,那么,如果小于等于3的数的个数如果超过了3,那么重复的数字一定出现在[1,3]之间,否则出现在[4,5]之间。以该数组为例,中位数为3,小于等于3的数一共有4个,大于3的数有两个,所以重复的数字在[1,3]之间。

代码:

int findDuplicate(vector<int>& nums) {
        // Write your code here
       int start=1;
       int end=nums.size()-1;
       while(start<end)
       {
           int mid=(start+end)/2;
           int l=0;
           
           for(int i=0;i<nums.size();i++)
           {
               if(nums[i]<=mid)l++;
              
           }
           if(l>mid)end=mid;
           else start=mid+1;
       }
       return start;




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值