[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]
Output: 2

Example 2:

Input: [3,1,3,4,2]
Output: 3

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.

寻找一个数组里的重复数,由于只能O(1)的空间复杂度,所以哈希表之类的就不能用了。

解法1: 双指针,寻找环。类似于 142. Linked List Cycle II

Treat each (key, value) pair of the array as the (pointer, next) node of the linked list, thus the duplicated number will be the begin of the cycle in the linked list. Besides, there is always a cycle in the linked list which starts from the first element of the array.
解法2: Binary Search

Java:

public int findDuplicate(int[] nums) {
    int slow = 0;
    int fast = 0;

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

    int find = 0;

    while(find != slow){
        slow = nums[slow];
        find = nums[find];
    }
    return find;
} 

Java:

public int findDuplicate(int[] nums) {
    int l = 1,r = nums.length - 1;
    while(l < r){
        int m = (l + r) / 2;
        int c = 0;

        for(int i: nums){
            if(i <= m){
                c++;
            }
        }

        //if c < m,
        if(c > m){
            r = m;
        }else{
            l = m + 1;
        }
    }

    return r;
}   

Python:

# Two pointers method
class Solution(object):
    def findDuplicate(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        slow = nums[0]
        fast = nums[nums[0]]
        while slow != fast:
            slow = nums[slow]
            fast = nums[nums[fast]]

        fast = 0
        while slow != fast:
            slow = nums[slow]
            fast = nums[fast]
        return slow

Python:

# Time:  O(nlogn)
# Space: O(1)
# Binary search method.
class Solution(object):
    def findDuplicate(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        left, right = 1, len(nums) - 1

        while left <= right:
            mid = left + (right - left) / 2
            # Get count of num <= mid.
            count = 0
            for num in nums:
                if num <= mid:
                    count += 1
            if count > mid:
                right = mid - 1
            else:
                left = mid + 1
        return left

Python:

# Time:  O(n)
# Space: O(n)
class Solution(object):
    def findDuplicate(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        duplicate = 0
        # Mark the value as visited by negative.
        for num in nums:
            if nums[abs(num) - 1] > 0:
                nums[abs(num) - 1] *= -1
            else:
                duplicate = abs(num)
                break
        # Rollback the value.
        for num in nums:
            if nums[abs(num) - 1] < 0:
                nums[abs(num) - 1] *= -1
            else:
                break
        return duplicate  

C++:

class Solution {
public:
    int findDuplicate(vector<int>& nums) {
        int low = 1, high = nums.size() - 1;
        while (low < high) {
            int mid = low + (high - low) * 0.5;
            int cnt = 0;
            for (auto a : nums) {
                if (a <= mid) ++cnt;
            }
            if (cnt <= mid) low = mid + 1;
            else high = mid;
        }
        return low;
    }
};

C++:

class Solution {
public:
    int findDuplicate(vector<int>& nums) {
        int slow = 0, fast = 0, t = 0;
        while (true) {
            slow = nums[slow];
            fast = nums[nums[fast]];
            if (slow == fast) break;
        }
        while (true) {
            slow = nums[slow];
            t = nums[t];
            if (slow == t) break;
        }
        return slow;
    }
};

 

类似题目:

[LeetCode] 142. Linked List Cycle II 链表中的环 II

原文地址:https://www.cnblogs.com/lightwindy/p/9602295.html

时间: 2024-12-11 14:52:16

[LeetCode] 287. Find the Duplicate Number 寻找重复数的相关文章

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];

287 Find the Duplicate Number 寻找重复数

一个长度为 n + 1 的整形数组,其中的数字都在 1 到 n 之间,包括 1 和 n ,可知至少有一个重复的数字存在.假设只有一个数字重复,找出这个重复的数字.注意:    不能更改数组内容(假设数组是只读的).    只能使用恒定的额外空间,即要求空间复杂度是 O(1) .    时间复杂度小于 O(n2)    数组中只有一个数字重复,但它可能不止一次重复出现.详见:https://leetcode.com/problems/find-the-duplicate-number/descri

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] 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 element must exist. Assume that there is only one duplicate number, find the duplicate one. Note: You must not modify t

LeetCode 287. Find the Duplicate Number

Find the Duplicate Number | LeetCode OJhttps://leetcode.com/problems/find-the-duplicate-number/ 这个题目属于编码比较简单但解法分析过程比较复杂. 首先,把1~n放入0~n个元素,必定有两个或以上元素重复.数字里没有0,所以从下标0出发不会再回到最初的元素0. 假设当前的下标为x,下一步的下标为f(x),若f(x) = A[x], 也即每次跳到一个元素,则下一步移动到当前元素值对应的元素下标.我们来证明

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(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

&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] ==