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)循环内环+相遇点到环入口点。于是可以从链表头和相遇点分别设一个 指针,每次各走一步,两个指针必定相遇,且相遇第一点为环入口点。 

【Unity】【2D】用连续图片生成动画Animation的简明步骤 本文介绍了Unity中使用2D连续图片制作2D动画Animation的简明步骤。 阅读详情

相关推荐

【QGIS】利用“创建网格”功能实现区域等距分割与坐标转换实战

本文详细介绍了如何在QGIS中使用“创建网格”工具实现区域等距分割。通过实战演示,将北京市行政区划切割为150米正方形网格,并重点讲解了关键步骤:选择投影坐标系EPSG:3857以确保网格尺寸精确,以及将结果转换为常用的WGS84经纬度坐标。文章还涵盖了进阶技巧与常见问题解决方案,帮助用户高效完成空间网格化分析任务。

weixin_29232507的博客 96

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

双目视觉入门实战:用Matlab标定相机,再到Python OpenCV完成测距的完整链路

本文详细介绍了双目视觉技术的完整实现流程,从Matlab相机标定到Python OpenCV测距的全链路实践。重点解析了Matlab与OpenCV的参数转换技巧,以及立体校正、视差计算等关键步骤的代码实现,帮助开发者快速掌握双目测距系统的搭建方法。

weixin_29166013的博客 448

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

Amesim基础篇-元件详解-流体库管路

Amesim基础篇-元件详解-流体库管路

2201_75375391的博客 4526

点云数据增强及预处理

数据增强: 1. random world flip:沿着x或者y轴,翻转点云 点云的x或者y,要翻转 3d box中心的x或者y,要翻转 3d box的朝向要翻转 point segmentation label,点云的相对顺序没有发生变化 2. random_world_rotation:沿着点云原点,旋转点云 点,x,y,绕z轴旋转 3d box,绕z轴旋转,中心和朝向都要变化 point segmentation label,点云的相对顺序没有发生变化 3. random_world_scaling

huang_victor的博客 4011

【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的博客 290

100天精通Python(可视化篇)——第89天:Bokeh库绘图可视化基础入门(参数说明+案例实战)

专栏导读 一、Bokeh是什么? 二、安装与导入 三、Bokeh接口介绍 四、创建图表 五、添加自定义渲染器 切换主题 添加图例 图例位置 图例方向 图例背景和边界 图例文本的外观 行列布局 网格布局

努力让自己发光,对的人才能迎着光而来 1万+

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

珍妮的选择的博客 250

基于Simulink的电动车PMSM能量泄放与回收系统仿真设计

摘要 本文设计了一个基于Simulink的电动车PMSM能量泄放与回收系统仿真模型。系统采用分层建模方案,包含永磁同步电机(PMSM)矢量控制、电池管理和泄放功率计算等核心模块。通过详细的参数计算和MATLAB脚本实现,模拟了电动车在制动时的能量回收过程。重点研究了泄放电阻的控制策略,提出基于母线电压和电池SOC的多目标优化算法,确保系统在保护硬件安全的同时最大化能量回收效率。仿真模型包含完整的数学推导和模块搭建指南,为电动车能量管理系统设计提供了有效的仿真验证平台。

huanghm88的专栏 195

五十天精通硬件设计第6天-放大器知识及选型

运算放大器(Operational Amplifier,简称运放)是一种高增益的直流耦合差分放大器,广泛应用于信号处理、滤波、比较、积分、微分等电路中。

whm128的博客 597

Leetcode287. Find the Duplicate Number

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

magic_jiayu的博客 231

药物对接仿真软件:HADDOCK_(16).实践项目:抗体-抗原对接案例

定义分子结构文件。

kkchenjj的博客 582

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

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

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,

无名山丘,崛起成峰 1489

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

[python 刷题] 287 Find the Duplicate Number

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

GoldenaArcher的博客 440

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

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逆天耗子 778

【算法】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...

小树屋 659

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的专栏 202

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的博客 539

[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 406

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、付费专栏及课程。

余额充值