287. Find the Duplicate Number找环的起点

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.
Note:
1. You must not modify the array (assume the array is read only).
2. You must use only constant, O(1) extra space.
3. Your runtime complexity should be less than O(n2).
4. There is only one duplicate number in the array, but it could be repeated more than once.

分析:假设数组中没有重复,那我们可以做到这么一点,就是将数组的下标和1到n每一个数一对一的映射起来。比如数组是213,则映射关系为 0->2,1->1,2->3,,假设这个一对一的映射关系是一个函数f(n),其中n是下标,f(n)是映射到的数。如果我们从下标为0出发,根据这个函数计算一个值,以这个值为新的下标,再用这个函数计算,以此类推,一直到下标超出数组边界。实际上可以产生一个类似链表一样的序列,比如在这个例子中产生的序列为0->2->3
但是如果数组中有重复的话,这中间就会产生多对一的映射,比如数组2131,它们的映射关系为0->2,1->1,2->3,3->1。这样子,我们的推演序列就一定会有环路了,该数组的序列为:0->2->3->1->1->1->…,而环的起点就是重复的数,我们在假设一个数组2311,这里的映射关系为0->2,1->3,2->1,3->1,该数组的序列如下:0->2->1->3->1->3->1->…
因此该题实际上就是找环路起点的问题,分别定义快慢指针,两个下标都从0开始,快下标每轮映射两次 ,慢下标每轮映射一次,直到两个下标再次相同。这时候保持慢下标位置不变,再用一个新的下标从0开始,这两个下标都继续每轮映射一次,当这两个下标相遇时,就是环的起点,也就是重复的数。

class Solution {
public:
    int findDuplicate(vector<int>& nums) {
        if(nums.size()<=1) return -1;
        int fast=0;
        int slow=0;
        do {
            slow=nums[slow];
            fast=nums[nums[fast]];    //此处不能用while循环,而必须先执行一次,然后再进行判断
        }while(fast!=slow);
        fast=0;
        while(fast!=slow)
        {
            fast=nums[fast];
            slow=nums[slow];
        }
        return slow;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值