287. Find the Duplicate Number(链表判环)

[Leetcode]287. Find the Duplicate Number简单解法及解释 双指针复杂度O(n) 题目链接: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 阅读详情

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.哈希表

对于这种数组中找重复元素的问题,我当然第一时间想到哈希表,但是哈希表空间复杂度是O(n),所以不能用。

2.暴力法

两个循环就可以暴力查找出重复的元素,时间复杂度是O(n^2),题目要求less than O(n^2),所以排除。

4.排序

时间是可以,但会改变数组,所以不可取。

5.二分法

我想到了二分法,但是刚开始我思路走入了误区。一共n+1个元素,每个元素都是在[1..n]范围之内,那么除过一个重复的,剩下的肯定就是无重复的且范围在[1..n]之内的元素了。

况且,这道题二分法和一般的不一样,这道题的思想是这样的。既然有元素值的范围,我们对元素值的范围进行二分,而不是对数组二分,然后通过统计数组中元素个数来加上限制条件,缩小元素值范围继续二分下去。

比如:对于元素集合[1..10],现在有n+1个元素,即10+1=11个,那么必定有一个重复元素。

我们首先将元素集合分为[1..5] 和 [6..10],此刻mid=5。通常来说,如果遍历数组得到<=5的元素数目和mid相等,则说明重复元素在右半区间中。否则,在左半区间中。然后继续二分下去,直到最终只有一个元素。例如:元素4重复了,那么数组中会有1,2,3,4,4,5,一共6个元素,这不正说明重复元素在mid即5的左半区间吗?

由于一直二分下去,最终剩下两个元素的时候,high会向low靠拢,所以我们返回low即可。

代码如下:

class Solution {
public:
    int findDuplicate(vector<int>& nums) {
        size_t size = nums.size();
        if(size ==0 || size == 1)
            return -1; 
    
        return binsearch_like(nums, size, 1, size-1);
    }   
private:
    int binsearch_like(vector<int>& nums, int size, int low, int high){
        while(low < high){
            int mid = low + (int)((high-low)>>1);
            int count = 0;
            for(int i=0; i<size; ++i){
                if(nums[i] <= mid)
                    ++count;
            }   
            if(count > mid)
                high = mid;
            else
                low = mid+1;
        }   
        return low;
    }
};


4.映射找环法

对于数组a[3] = {1, 2, 3},我们把它看作下标和元素值的映射。0->1, 1->2, 2->3, 连起来就是 0->1->2->3。

既然存在重复元素,例如数组a[5] = {1, 2, 3, 3, 4}, 连起来就是 0->1->2->3->3->3...无限环转圈

或者数组a[6] = {1, 2, 4, 5, 3, 3},连起来就是0->1->2->4->3->5->3->5->3->5->3....同样无线循环转圈

这道题这就转换成了链表求环的问题。

这种题的通常解法就是一个快指针,一个慢指针。快指针步长为满指针的2倍。就好像在操场跑步,一个人跑的快,一个人跑的慢,只要操场是个环,那么跑的快的总会追上跑得慢的。追上的点就是有环点。

由于步长的

然后再定义一个指针从0开始,之前的指针从有环点开始,两个相遇点即为重复元素所在。

代码如下:

class Solution {
public:
    int findDuplicate(vector<int>& nums) {
        size_t size = nums.size();
        if(size ==0 || size == 1)
            return -1; 
    
        int slow = 0;
        int fast = 0;
        do{
            slow = nums[slow];
            fast = nums[nums[fast]];
        }while(slow != fast);
        
        int find = 0;
        while(find != slow){
            find = nums[find];
            slow = nums[slow];
        }
        return slow;
    }   
};

证明如下:

如果fast和slow相遇,那么在相遇时,slow肯定没有遍历完链表,而fast已经在环内循环了n圈(n>=1)(见下面的证明2)。假设 slow走了s步,则fast走了2s步(fast的步数还等于s加上在环上多转的n圈),设环长为r,则: 
2s=s+nr 
s=nr 
设整个链表长L,入口环与相遇点距离为x,起点到环入口点的距离为a,则 
a+x=s=nr 
a+x=(n-1)r+r=(n-1)r+L-a 
a=(n-1)r+(L-a-x) 
(L-a-x)为相遇点到环入口点的距离。由此可知,从链表头到环入口点等于(n-1)循环内环+相遇点到环入口点。于是可以从链表头和相遇点分别设一个 指针,每次各走一步,两个指针必定相遇,且相遇第一点为环入口点。 

RDkit入门 今天开始写RDkit的入门教程,每天学习和写一小点,相信一句话“不积跬步无以至千里,不积小流无以成江海”,所以就慢慢来,一点一滴积累叭,从最简单的开始喽希望自己每天都能保持写博客的好习惯,把自己的代码笔记就极在这里,RDkit一块啃了很久的骨头,今天终于要下定决心,好好总结一波了,希望保持这个更新的频率,每天都能有所学。............ 阅读详情

相关推荐

cut&tag学习笔记

上图是我得到的结果,下图是教程的结果,比对结果有一些差异,我也不知道具体原因是啥。谷歌了一下,发现原因可能是因为环境变量的问题,系统不知道picard的具体位置,需要设置全局环境变量,遂查了一下picard的位置。我发现我做出来的结果测序深度一致,但是比对的结果有一点差异,不知道是我的操作问题,还是参考基因组更新了导致比对结果出现了些许不同。上图是我得到的结果,下图是教程的结果,除了IgG_rep1外比对结果一致。上图是我的结果,下图是教程的结果,结果不太一致,不知道是具体的原因是什么。

weixin_52073820的博客 2437

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) 。 数组中只有一个重复的数字,但它可能不止重复出现一次。 来源:力扣(LeetCode) 链接:https:...

qinian_ztc的博客 180

以太网物理层协议整理(2)-万兆以太网

在万兆以太网标准化过程中,IEEE和10GEA(万兆以太网联盟)是两个最重要的组织。 万兆以太网标准和规范都比较繁多,在标准方面,首先有2002年的IEEE 802.3ae,后来也不断有新的万兆以太网规范推出,如2006年的IEEE 802.3an标准(10GBase-T)就推出了基于双绞铜线的万兆以太网规范,同样的IEEE 802.3aq标准推出了基于光纤的10GBase-LRM;2007年的

NowOrNever 3万+

LeetCode 287. Find the Duplicate Number(链表判环)

287. Find the Duplicate Number Medium 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 ...

da_kao_la的博客 254

python123题目——摩尔斯电码转换

类型:列表元组。

m0_62488776的博客 4445

cdk&java | 绘制含有丰富信息的化学反应(适合论文、网页、PPT)

package org.example; import org.openscience.cdk.DefaultChemObjectBuilder; import org.openscience.cdk.depict.DepictionGenerator; import org.openscience.cdk.exception.CDKException; import org.openscience.cdk.interfaces.IReaction; import org.openscience.cdk.

ASKCOS博客 908

【Leetcode】287. Find the Duplicate Number(查找数组中唯一一组相同数字)(单链表判环)

Given an arraynumscontainingn+ 1 integers where each integer is between 1 andn(inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, fi...

小I的博客 292

《使用R包ggtree进行进化树的绘制与美化》

都2023年了,才去使用Y叔早在2018年发表的进化树构建利器——ggtree。刚好最近在做基因家族分析,进化树的构建与美化确实有点麻烦,先前使用MEGA11进行了绘制,但图像简陋,于是导出了nwk文件,准备使用其他软件进行美化。这里,我使用ggtree包进行操作。

沉香best的博客 6907

287. **Find the Duplicate Number

287. **Find the Duplicate Number https://leetcode.com/problems/find-the-duplicate-number/description/ 题目描述 Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusiv...

珍妮的选择的博客 252

YOLOv11改进 | 模型知识蒸馏篇 | 利用模型蒸馏改进YOLOv11进行无损涨点 | 在线蒸馏 (附代码 + 完整文件 + 解析教程)

这篇文章给大家带来的最新改进是模型的蒸馏,利用教师模型指导学生模型从而进行模型的涨点,本文的内容不仅可以用于论文中,在目前的绝大多数的工作中模型蒸馏是一项非常重要的技术,所以大家可以仔细学习一下本文的内容,本文从YOLOv11的项目文件为例,进行详细的修改教程,文章内包括完整的修改教程,针对小白我出了视频修改教程,如果你还不会我提供了修改后的文件大家直接运行即可,所以说不用担心不会适用!模型蒸馏真正的无损涨点,蒸馏你只看这一篇文章就足够了,本文内容为在线蒸馏教程,之前的文章为离线蒸馏!

Snu77的博客 2690

回归预测 | MATLAB实现MPR多元多项式回归

回归预测 | MATLAB实现MPR多元多项式回归

机器学习之心的博客,关注并私信文章链接,获取对应文章源码和数据。 1651

Leetcode287. Find the Duplicate Number

假设一个数组中只有一个重复的整数,其余元素各不相同,找出这个同步的数。不可以更改原数组,要求空间复杂度为O(1)。

magic_jiayu的博客 231

这个AI工具可以把产品说明书转成演示视频,老板狂喜

回顾本文,我们从“传统视频制作的痛点”出发,拆解了AI将产品说明书转为视频的核心技术(NLP文本理解→CV画面生成→视频合成),并提供了3类工具实操方案(SaaS/开源/API)。推广成本降80%,制作效率提90%,产品迭代与推广终于能“同频”;摆脱对专业团队的依赖,自己就能快速产出高质量视频;对技术爱好者来说,这是一个“AI多模态融合”的绝佳实践案例(NLP+CV+语音合成的综合应用)。今天就找一份产品说明书,用HeyGen或Pictory试做一个视频。

AIGC应用创新大全的博客 335

LeetCode Top 100 Liked Questions 287. Find the Duplicate Number (Java版; Medium)

welcome to my blog LeetCode Top 100 Liked Questions 287. Find the Duplicate Number (Java版; Medium) 题目描述 Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive),...

littlehaes的博客 258

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

inori mmd 7z

楪祈的模型资源....................................................................................................................................................................................................................

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,

无名山丘,崛起成峰 1491

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

[python 刷题] 287 Find the Duplicate Number

题目:numscontainingn + 1[1, n]inclusive.innums, returnwithoutnums。

GoldenaArcher的博客 444

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

LeetCode 287. Find the Duplicate Number

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

i逆天耗子 780

【算法】Leetcode 287 Find the Duplicate Number 找重复元素

目录题目随想思路及代码方法1 哈希表优点:缺点:方法2 排序优点:缺点:方法3 二分查找优点:缺点:代码方法4 利用索引交换优点:缺点:代码:方法5 链表判环优点:缺点:代码: 题目 原题链接 Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove...

小树屋 660

Find the Duplicate Number leetcode 287

题目大意:找到数组中唯一的一个重复的数值。时间复杂度要小于O(n^2),不能修改数组了。 发现一个很6的算法,把数组当成一个静态链表,也就是说nums[],数组相当于next[],0为起始节点,节点地址范围就是0-n,一共n+1个节点。 因为数组范围是1-n,所以没有节点next会指向0。

天天烤代码 661

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, fi...

qq_34229391的博客 411

LeetCode: 287. Find the Duplicate Number

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. Assu...

杨领well的专栏 204

Leetcode算法学习日志-287 Find the Duplicate Number

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. A

Zarlove的博客 542

[leetcode] 287. 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. Assume that there is only one duplicat...

Learning from the mistakes 408

leetcode 287. Find the Duplicate Number | 287. 寻找重复数(判断链表是否有环,并找到环的起点)

题目 https://leetcode.com/problems/find-the-duplicate-number/ 题解 题目有限制 不能修改数组元素,必须 O(1) 空间复杂度,所以 不能排序,不能用 set / map 因为本题具有 所有数字都在 1 到 n 之间 的特点,所以天然的(出题人故意的)可以当做一个链表来处理。 官方题解利用了与 “找到环形链表的第一个起点” 相同的思路。 Approach 3: Floyd’s Tortoise and Hare (Cycle Detection) I

寒泉 5万+

LeetCode 287. Find the Duplicate Number 题解(C++)

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 dup

prayyojay的博客 567

[LEETCODE] 287. Find the Duplicate Number

[LEETCODE] 287. Find the Duplicate NumberGiven 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 tha

TonyYang1995的博客 1255
上一篇: 87. Scramble String
下一篇: 219. Contains Duplicate II
FreeeLinux
博客等级 码龄11年 421粉丝 356原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址