LeetCode:寻找重复数【287】

LeetCode:寻找重复数【287】

题目描述

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

示例 1:

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

示例 2:

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

说明:

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

题目分析

  这道题由于说明中对时间复杂度和空间复杂度的限制,我们采用了龟兔相遇求环入口的方式,这种方式的的前提是我们要把一个数组看做一个又环链表,元素的值标识下一个元素的位置,元素的下表标识该元素的位置

  如果你能理解上面这段话的意思的话,我们可以看看入口的求解方式:

  

Java题解

class Solution {
    public int findDuplicate(int[] nums) {
        if(nums.length>1)
        {
            int slow = nums[0];
            int 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;
        }
        return -1;

    }
}

原文地址:https://www.cnblogs.com/MrSaver/p/9557479.html

时间: 2024-08-08 05:00:17

LeetCode:寻找重复数【287】的相关文章

[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寻找重复数

这道题用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];

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) . 数组中只有一个重复的数字,但它可能不止重

[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

287 Find the Duplicate Number 寻找重复数

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

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寻找两个有序数组的中位数

题目: 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2 不会同时为空. 示例 1: nums1 = [1, 3]nums2 = [2] 则中位数是 2.0 示例 2: nums1 = [1, 2]nums2 = [3, 4] 则中位数是 (2 + 3)/2 = 2.5 来源:力扣(LeetCode) 链接:https://leetcode-cn.

LeetCode-287 寻找重复数

传送门:https://leetcode-cn.com/problems/find-the-duplicate-number/submissions/给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数.假设只有一个重复的整数,找出这个重复的数. 示例 1: 输入: [1,3,4,2,2]输出: 2示例 2: 输入: [3,1,3,4,2]输出: 3说明: 不能更改原数组(假设数组是只读的).只能使用额外的 O(1) 的空

leecode第二百八十七题(寻找重复数)

class Solution { public: int findDuplicate(vector<int>& nums) { int slow_index= nums[0]; int quick_index= nums[0]; do { slow_index=nums[slow_index]; quick_index=nums[nums[quick_index]]; }while(slow_index!=quick_index);//可以理解成一个状态向量机,状态转移过程中一定会有环