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

RTLocating框架:硬件设计迭代的精准定位技术 在芯片设计领域,寄存器传输级(RTL)代码是数字电路行为描述的核心语言,其开发效率直接影响芯片项目的整体周期。传统RTL开发面临的主要挑战在于代码修改时的精准定位问题,尤其在大型SoC设计中更为突出。RTLocating框架通过融合自然语言处理技术与硬件设计知识,实现了从变更需求描述到具体RTL代码块的精准定位。该框架采用多维度编码器融合、动态路由机制等创新技术,显著提升了硬件设计迭代的效率。RTLocating特别适用于功能扩展、微架构优化和错误修复等典型场景,为EDA工具集成提供了高效的解决方案。通过工 阅读详情

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:

You must not modify the array (assume the array is read only).
You must use only constant, O(1) extra space.
Your runtime complexity should be less than O(n2).
There is only one duplicate number in the array, but it could be repeated more than once.

给出一个包含n+1个整数的数组,包含1~n(包含n),找出重复的数字

附加条件:
不能修改数组,也就是不能用排序
只能用O(1) 的额外空间,也就是不能用hashset之类的保存数字
runtime小于O(n2),也就是不能用brute force搜索

思路:
本质上是链表中存在环,找环入口的问题。

为什么是链表的环?且把数组中每个数字看作数组下标(因为本来应该是1~n的下标,对应1~n的数字)来看。
以前在链表中走一步是把指针右移,即指针++,
这里的走一步是找到nums中对应下标的数字,即nums[pointer],走两步就是nums[nums[pointer]]。

可以看example1: [1,3,4,2,2]
从下标0开始,nums[0] = 1, 然后nums[1] = 3, nums[3] = 2, nums[2] = 4, nums[4] = 2,
看到这里出现了环,nums[3] == nums[4]。

找到环的入口在142. 链表环中见过,
用快慢指针,快指针每次走两步,慢指针每次走一步,它们相遇就是存在环。
然后慢指针回到起点,快慢指针每次都走一步,它们相遇的地方即是环的入口,也就是重复的元素。

    public int findDuplicate(int[] nums) {
        int n = nums.length;
        int slow = 0;
        int fast = 0;
        
        while(true) {
            slow = nums[slow];
            fast = nums[nums[fast]];
            if(slow == fast) break;
        }
        
        slow = 0;
        
        while(true) {
            slow = nums[slow];
            fast = nums[fast];
            if(slow == fast) break;
        }
        return slow;
    }

参考 Floyd算法

[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 阅读详情

相关推荐

SOI技术解析:绝缘体上硅的结构优势与应用场景

本文深入解析了SOI(绝缘体上硅)技术的核心结构、性能优势与应用场景。SOI通过在硅衬底与器件层之间引入绝缘的埋氧层,实现了优异的电隔离,从而显著降低芯片功耗、提升工作频率并增强抗干扰能力。该技术已广泛应用于5G射频前端、高性能计算、汽车电子及MEMS传感器等关键领域,是推动半导体性能持续提升的重要技术路径。

jj890的博客 409

[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

CMMI AIM 人工智能成熟度框架:31个过程域的157项AI实践集成

CMMI AIM人工智能成熟度框架为企业提供了系统化管理AI项目全生命周期的解决方案,包含31个过程域的157项AI实践。该框架通过四个演进阶段(混沌与风险、结构与凝聚、验证与信任、价值与增长),帮助组织建立可持续的AI能力成长路径,降低项目失败率并提升商业价值。

weixin_42613501的博客 375

Find the Duplicate Number寻找重复数(剑指offer,寻找重复的数)

给定一个包含n+ 1 个整数的数组nums,其数字都在 1 到n之间,包括 1 和n,可知至少存在一个重复的整数。假设只有一个重复的整数,找出这个重复的数。 示例 1: 输入: [1,3,4,2,2] 输出: 2 示例 2: 输入: [3,1,3,4,2] 输出: 3 说明: 不能更改原数组(假设数组是只读的)。 只能使用额外的 O(1) 的空间。 时间...

qq_26410101的博客 790

告别单一帧:在CFD-POST中完整加载Fluent瞬态计算结果的实战指南

本文详细介绍了在CFD-POST中完整加载Fluent瞬态计算结果的实战指南,解决工程师常见的只能看到最后一帧数据的问题。通过Fluent端的正确设置和CFD-POST端的完整加载技巧,帮助用户高效处理瞬态数据,提升后处理效率。文章还提供了常见问题排查、性能优化和高级后处理技巧,适用于Workbench环境下的流体流动分析。

weixin_29284657的博客 546

直流有刷电机控制 - 驱动电路(二)

1.如图所示为最常见的推挽驱动电路,我附加了具有正反转功能的电机驱动控制,正反转是由两个继电器组成。使用推挽电路来驱动NMOS管时充电电流和放电电流都很大,因此NMOS管导通快,关断也快,而且NMOS温度不会很高。下面来介绍一下该电路的原理。(1)PWM_MCU是由单片机给出的PWM波信号,该信号是由一些列高低电平的方波构成。当信号为高电平的时候Q3导通,经过R47再到Q5的g极,此时mos导通,如果是K1闭合是正转那么K2闭合就是反转。(2)当PWM低电平时使得Q3关断,Q6导通。

u011030644的博客 4738

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

430

肿瘤基因组分析实战:VAF、MAF、CCF这些指标到底怎么算?(附Python代码示例)

本文通过Python代码示例,详细解析了肿瘤基因组分析中VAF、MAF和CCF三大核心指标的计算逻辑与应用。文章从VCF文件解析入手,逐步演示了变异等位基因频率、次等位基因频率及癌细胞分数的计算方法,并探讨了拷贝数变异对结果的影响及克隆结构推断的实战流程,为生物信息分析提供实用指南。

silver的专栏 1013

Find the Duplicate Number leetcode 287

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

天天烤代码 661

Windows服务器SSL漏洞修复实战:从CVE-2016-2183到RC4的完整配置指南

本文提供了一份针对Windows服务器的SSL/TLS安全漏洞(如CVE-2016-2183和RC4相关漏洞)的实战修复指南。文章深入解析了漏洞原理,并详细演示了如何通过组策略编辑器、注册表和PowerShell命令,禁用不安全的密码套件(如3DES和RC4),配置安全的TLS协议版本,以加固服务器安全。同时,提供了配置验证、自动化管理和故障排除的完整流程,帮助运维人员有效应对安全扫描与合规要求。

yy01234的博客 1090

ClawManager实战:OpenClaw批量部署+精细化权限管控,运维效率拉满90%

ClawManager彻底解决了OpenClaw规模化部署的运维痛点,它不是简单的“批量部署工具”,而是一套完整的企业级管理方案——从模板配置、批量部署,到权限管控、监控审计、AI治理,覆盖OpenClaw全生命周期管理,让OpenClaw从“个人工具”升级为“企业级AI基础设施”。对于中小企业,Docker一键部署即可快速落地,无需复杂配置;对于大型企业,K8s部署支持高可用、横向扩展,结合Keycloak对接企业现有认证系统,可完美融入企业IT架构。

2601_94871597的博客 618

LeetCode 287. Find the Duplicate Number(找重复数字)

原题网址:https://leetcode.com/problems/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 nu

James Pan 3116

一个超酷的开源uHand2.0机械手掌项目

uHand2.0是深圳乐幻索尔公司开源的一款机械手掌,它长下面这个样子:1、uHand2.0外观图之前在公众号就分享过视频:学习嵌入式可以带娃,不信你们看2、uHand2.0硬件原理图看...

Bruce.yang的嵌入式之旅 4472

LeetCode //C - 287. Find the Duplicate Number

【代码】LeetCode //C - 287. Find the Duplicate Number。

Made in Code 803

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

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

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

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

IEM模型,地表粗糙度模拟

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

leetcode 287. Find the Duplicate Number,数组中找重复的数

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 th

记忆力不好的博客 2866

leetcode 287: Find the Duplicate Number

Find the Duplicate Number Total Accepted: 1340 Total Submissions: 4766 Difficulty: Hard Given an array nums containing n + 1 integers where each integer is between 1 and n (incl

西施豆腐渣 8802

Java描述 LeetCode,287. Find the Duplicate Number 寻找重复数

两种方法,从循环链表中找出思路

人在江湖 990

[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 element must exist. Assume that there is only one duplicate number,

ER_Plough的专栏 2355

LeetCode 287: Find the Duplicate Number题解(python)

Leetcode 287: Find the Duplicate Number 分类:Binary Search 难度:M 描述:给了一个长度为n + 1的数组,数组中的树大小在1和n之间。证明这个数组中一定有重复的数字,并且把它找出来。 Input: [1,3,4,2,2] Output: 2 Input: [3,1,3,4,2] Output: 3 链接: Find the Duplicate Number. 思路: 在这里首先说一个原理: 抽屉原理.简单来说,就是11个苹果想放进10个或更少的抽屉

Bourns_A的博客 405

[leetcode]287. Find the Duplicate Number

题目链接:https://leetcode.com/problems/find-the-duplicate-number/?tab=Description Given an arraynumscontainingn+ 1 integers where each integer is between 1 andn(inclusive), prove that at least

xiaocong1990的博客 415

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

leetcode 【287 Find the Duplicate Number】【Python】

本题同样让我们找数组中重复的数字,但是给出了很多限制条件。其实leetcode好像对一些限制条件,无法做出判断,我们还是自我严格要求的好哦!首先,第一个不可以改变数组,其实这个就是限制了我们排序,如果排序,然后再遍历一下很快就能找到重复的数字了,时间复杂度O(nlogn),空间复杂度O(1); 其次,空间复杂度O(1),这个限制了我们使用hash查找,因为hash时间复杂度是O(1),但是空间要O

u014251967的博客 2583

[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

【算法】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
上一篇: leetcode 278. First Bad Version (第一个bad version)
下一篇: leetcode 229. Majority Element II(主元素II)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值
⌂ 换网址