leetcode 287. 寻找重复数(Find the Duplicate Number)

目录

  • 题目描述:
  • 示例 1:
  • 示例 2:
  • 解法:

题目描述:

给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数。假设只有一个重复的整数,找出这个重复的数。

示例 1:

输入: [1,3,4,2,2]
输出: 2

示例 2:

输入: [3,1,3,4,2]
输出: 3

说明:

  • 不能更改原数组(假设数组是只读的)。
  • 只能使用额外的 O(1) 的空间。
  • 时间复杂度小于 O(n2) 。
  • 数组中只有一个重复的数字,但它可能不止重复出现一次。

解法:

class Solution {
public:
    // method 1: could change the vector
    int findDuplicate1(vector<int>& nums) {
        int n = nums.size()-1;
        int i = n;
        while(i >= 0){
            if(i == nums[i]){
                i--;
            }else{
                if(nums[i] == nums[nums[i]]){
                    return nums[i];
                }else{
                    swap(nums[i], nums[nums[i]]);
                }
            }
        }
        return -1;
    }

    // method 2: not change the vector
    int findDuplicate2(vector<int>& nums) {
        int n = nums.size()-1;
        int fast = 0, slow = 0;
        do{
            fast = nums[fast];
            fast = nums[fast];
            slow = nums[slow];
        }while(nums[fast] != nums[slow]);
        fast = 0;
        while(nums[fast] != nums[slow]){
            fast = nums[fast];
            slow = nums[slow];
        }
        return nums[fast];
    }

    int findDuplicate(vector<int>& nums) {
        return findDuplicate2(nums);
    }
};

原文地址:https://www.cnblogs.com/zhanzq/p/10932259.html

时间: 2024-08-01 06:16:13

leetcode 287. 寻找重复数(Find the Duplicate Number)的相关文章

LeetCode:寻找重复数【287】

LeetCode:寻找重复数[287] 题目描述 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数.假设只有一个重复的整数,找出这个重复的数. 示例 1: 输入: [1,3,4,2,2] 输出: 2 示例 2: 输入: [3,1,3,4,2] 输出: 3 说明: 不能更改原数组(假设数组是只读的). 只能使用额外的 O(1) 的空间. 时间复杂度小于 O(n2) . 数组中只有一个重复的数字,但它可能不止重复出现一次.

287. 寻找重复数

题目描述 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数.假设只有一个重复的整数,找出这个重复的数. 示例 1: 输入: [1,3,4,2,2] 输出: 2 示例 2: 输入: [3,1,3,4,2] 输出: 3 说明: 不能更改原数组(假设数组是只读的). 只能使用额外的 O(1) 的空间. 时间复杂度小于 O(n2) . 数组中只有一个重复的数字,但它可能不止重复出现一次. 题意 审题可以发现两个关键点: num

leetcode Find the Duplicate Number

题目连接 https://leetcode.com/problems/find-the-duplicate-number/ Find the Duplicate Number 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.

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

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], 也即每次跳到一个元素,则下一步移动到当前元素值对应的元素下标.我们来证明

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