LeetCode 287. Find the Duplicate Number

Find the Duplicate Number | LeetCode OJ
https://leetcode.com/problems/find-the-duplicate-number/

这个题目属于编码比较简单但解法分析过程比较复杂。

首先,把1~n放入0~n个元素,必定有两个或以上元素重复。数字里没有0,所以从下标0出发不会再回到最初的元素0。

假设当前的下标为x,下一步的下标为f(x),若f(x) = A[x], 也即每次跳到一个元素,则下一步移动到当前元素值对应的元素下标。我们来证明这样的移动存在环:

假设重复的元素值为x.

假设x存在于A[1..n]中,A[i] 和A[j]的值为x。除去i,j,A[1..n]还剩下n-2个元素,若A[x]值为i,j,x之一,显然这是一个环。假设A[x]的值为k,k!=x && k!=i && k!=j,则剩下n-3个元素。因为重复的元素只有x,所以剩下的元素值不能为k,这样我们只有n-4个不重复的值可用,如果要填入n-3个元素,或者这n-4个元素有重复,这和假设矛盾,或者只能填入i,j,x之一。这样就形成了一个环。

A[0]=x的情况证明也类似。

既然证明了这种遍历方式存在环,我们就可以套用 Linked List Cycle II 的思路来找到环的起点。此处不赘述。

详细分析可以参考这篇:http://keithschwarz.com/interesting/code/?dir=find-duplicate

代码如下:

class Solution {
public:
    int findDuplicate(vector<int>& nums) {
        if(nums.size() <= 1){
            return -1;
        }

        int fast = 0, slow = 0;
        do{
            fast = nums[nums[fast]];
            slow = nums[slow];
        }while(fast != slow);

        fast = 0;
        while(fast != slow){
            fast = nums[fast];
            slow = nums[slow];
        }

        return slow;
    }
};

 

时间: 2024-07-29 02:31:19

LeetCode 287. Find the Duplicate Number的相关文章

LeetCode 287. Find the Duplicate Number (python 判断环,时间复杂度O(n))

LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过不了的,但是可能判题并没有卡空间复杂度,所以也能AC. class Solution: # 基本思路为,将第一次出现的数字 def findDuplicate(self, nums: List[int]) -> int: s = set() for i in nums: a = i in s if a

LeetCode 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: You must not modify th

[LeetCode] 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. Example 1: Input: [1,3,4,2,2

[LeetCode] 287. Find the Duplicate Number(Floyd判圈算法)

传送门 Description 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: You mu

leetcode 287 Find the Duplicate Number寻找重复数

这道题用STL容器就很好写了,可以用set也可以用map, 用unordered_map的C++代码如下: 1 class Solution { 2 public: 3 int findDuplicate(vector<int>& nums) { 4 unordered_map<int, int> m; 5 int res; 6 for(int i=0;i<nums.size();i++){ 7 if(m.count(nums[i])){ 8 res=nums[i];

&lt;LeetCode OJ&gt; 287. Find the Duplicate Number

287. Find the Duplicate Number My Submissions Question Total Accepted: 18097 Total Submissions: 48596 Difficulty: Hard Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate

leetcode 217. Contains Duplicate 287. Find the Duplicate Number

217. Contains Duplicate 后面3个题都是限制在1-n的 class Solution { public: bool containsDuplicate(vector<int>& nums) { int length = nums.size(); if(length <= 0) return false; sort(nums.begin(),nums.end()); for(int i = 1;i < length;i++){ if(nums[i] ==

287. Find the Duplicate Number *HARD*

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: You must not modify th

287. Find the Duplicate Number

https://leetcode.com/problems/find-the-duplicate-number/#/description http://www.cnblogs.com/EdwardLiu/p/5078013.html range search : The reason why we did not use index as "search space" for this problem is the matrix is sorted in two directions