Leetcode NO.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 阅读详情

1.问题描述

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

假设 nums 只有 一个重复的整数 ,找出 这个重复的数 。

你设计的解决方案必须不修改数组 nums 且只用常量级 O(1) 的额外空间。

2.测试用例

示例 1:

​ 输入:nums = [1,3,4,2,2]
​ 输出:2

示例 2:

​ 输入:nums = [3,1,3,4,2]
​ 输出:3

示例 3:

​ 输入:nums = [1,1]
​ 输出:1

示例 4:

​ 输入:nums = [1,1,2]
​ 输出:1

3.提示

  • 1 <= n <= 105
  • nums.length == n + 1
  • 1 <= nums[i] <= n
  • nums 中 只有一个整数 出现 两次或多次 ,其余整数均只出现 一次

4.代码

1.binary search
code
public int findDuplicate4(int[] nums) {
        int n = nums.length;
        if (n < 1) {
            return -1;
        }
        //取值范围是从1 -N
        //从1开始
        int l = 1;
        //nums.length 是 n + 1; 结束到N
        int r = nums.length - 1;

        while (l <= r) {
            int mid = (l + r) / 2;
            int count = 0;
            for (int num : nums) {
                if (num <= mid) {
                    count++;
                }
            }
            if (count <= mid) {
                l = mid + 1;
            } else {
                r = mid;
            }
            if (l == r) {
                return l;
            }
        }
        return -1;
}
复杂度

时间 O(nlogn)
空间 O(1)

2. 快慢指针
code
public int findDuplicate(int[] nums) {
        int low = 0, fast = 0;

        do {
            low = nums[low];
            fast = nums[nums[fast]];
        } while (low != fast);


        int p1 = 0, p2 = low;
        do {
            p1 = nums[p1];
            p2 = nums[p2];
        } while (p1 != p2);

        return p1;
}
复杂度

时间 O(n)
空间 O(1)

西门子S7-1200与汇川SV660F伺服Profinet通讯调试实战 在工业自动化领域,Profinet作为工业以太网标准协议,凭借其高实时性和灵活组网能力,已成为伺服驱动器与PLC通讯的主流选择。其核心原理是通过设备名识别、IP分配以及标准报文(如111速度报文、105定位报文)传输控制字与状态字,实现驱动器的精准控制。相比传统脉冲或CANOpen方案,Profinet直连省去了协议转换网关,降低了成本与故障点,尤其适用于绕线设备、包装机械等中小型单轴或多轴运动控制系统。本文以西门子S7-1200 PLC搭配汇川SV660F伺服为例,完整梳理了从GSDMD组态、InoDri 阅读详情

相关推荐

VLM,VLA,VLN的区别与联系

VLM 提供了理解世界的“眼睛和大脑”,VLN 赋予了这个智能体在空间中“移动的双腿”,而 VLA 则进一步赋予了它“灵巧的双手”来改变世界。三者共同构成了一个完整具身智能体的核心能力模块。

l0409044222的博客 1313

LeetCode No.287 Find the Duplicate Number

LeetCode No.287 Find the Duplicate Number

黄健伟的博客 379

springboot + netty 实现多客户端登录 超出设备 并下线

本文档旨在详细描述多平台登录控制的需求和实现方案。当前系统仅支持单设备登录,需扩展为支持多设备同时在线,并提供多种登录策略。文档详细列出四种策略:单平台登录、双平台登录、三平台登录和多平台同时在线,每种策略的具体限制和支持平台。技术实现方案包括数据结构设计、NettyUtils 工具类方法、WebSocketHandler 修改,以及相应的测试方案

Likefr 1878

No.246 - LeetCode[287] Find the Duplicate Number - 数组值在1-n内找唯一重复值

这个数组可以形成有环的单向链表,数组下标是当前点,数组值是指向下一节点的指针 假设环外长d,环长l,相碰处距汇点x 则fast = d+l+x,slow = d+x,且fast = 2slow 解得x = l - d,即slow再走d步就是汇点。 第一次初始化假设f和s都各先走了一步, s= 0 , s = nums[s] 即 s = nums[0] f = 0, f = nums[nums[f]] 即 f = nums[nums[0]] 故f第二次初始化为0 /* * @lc app=leetcode

ShellDawn的博客 197

PCIe协议之 Equalization篇 之 理论篇 之 FFE

作为最后一道“精加工”工序,DFE利用其不放大噪声的优势,精准地消除残余的、强烈的后光标ISI,最终得到一个非常干净、张得很开的眼图,供判决器使用。信号经过信道衰减后变得模糊,首先由CTLE进行“粗调”,提供一个宽频带的增益,将眼图初步打开,为后面的DFE创造一个可以正常工作的信号环境。,它通过将输入信号在不同时间点上的多个副本(称为“抽头”)进行加权求和,来直接塑造或“整形”输出信号的波形。:这是最主要的信号路径,通常系数为正值,决定了信号的幅度。:将所有加权后的信号相加,产生最终的均衡输出。

那么菜的博客 755

通过Simulink搭建一个智能微电网能量管理系统(Energy Management System, EMS)

通过上述代码实现,您可以逐步创建一个智能微电网能量管理系统。多源能量集成:整合光伏、风力发电、柴油发电机、电池储能等多种能源资源。负荷预测与调度:基于历史数据和天气预报,预测未来负荷和可再生能源输出,进行优化调度。储能系统优化:设计合理的充放电策略,确保储能系统的高效利用和寿命延长。经济性分析:评估不同运行模式下的经济性,考虑电价、维护成本等因素。环境影响评估:分析微电网对环境的影响,如减少碳排放、提高可再生能源利用率等。硬件在环仿真(HILS)

amy_mhd的博客 314

Leetcode287. Find the Duplicate Number

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

magic_jiayu的博客 231

YOLOv11 热力图可视化全流程:从模型解释到科研论证,让检测结果“有理可看”

无论是科研中需要严谨的模型解释,还是工业里要高效的缺陷定位,这套方法都能让你在“模型黑箱”与“业务需求”之间架起一座可视化的桥梁。别再让模型决策“无据可依”——现在就动手生成你的第一幅热力图,让 YOLOv11 的每一次检测都“有理可看”,用可视化的力量推动你的 AI 研究与落地走向深入!某团队在论文中通过热力图对比,证明其改进的注意力模块可使模型对小目标的关注区域更精准,实验论证说服力提升 50%;工业质检场景下,热力图帮助工程师快速定位模型漏检缺陷的原因——原来是对某类缺陷的特征关注不足。

m0_71589036的博客 677

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

什么是 Agent 的 Context Window?为什么它是 Agent 工程中最核心的约束之一?

什么是 Agent 的 Context Window?为什么它是 Agent 工程中最核心的约束之一?

我哎GIS博客 818

【Flutter 问题系列第 16 篇】如何防止软键盘弹出后顶掉内容

Flutter 如何防止软键盘弹出后顶掉内容

“Allen Su”的博客 3317

LeetCode //C - 287. Find the Duplicate Number

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

Made in Code 802

数字逻辑Fundamentals of Digital Logic with Verilog Design | 3rd Edition Solutins Chapter 3(step by step)

第三章3-1~3-27全部习题解析和代码模块

weixin_62651190的博客 1946

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

漫漫学习路 490

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

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

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

[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

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

LeetCode-287. Find the Duplicate Number [C++][Java]

Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive. There is only one repeated number in nums, return this repeated number.

贫道绝缘子的博客 960

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

西施豆腐渣 8799

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

[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的专栏 2354

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

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

LeetCode 287 最简单的想法就是先排序,然后遍历去检查。 def findDuplicate2(self, nums: List[int]) -> int: nums.sort() for x in range(1, len(nums)): if nums[x] == nums[x-1]: return nums[x] 但是题目里面给出了两个限制: You must not modify th

frankguodongchen的博客 190

[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的博客 1254

LeetCode 287 Find the Duplicate Number

LeetCode 287 Find the Duplicate Number 题意 给一个长度为n+1的数组,数组中元素的值再1到n之间(包括1和n),其中有一个元素重复出现了多次,让你求出这个元素。要求时间复杂度小于O(n^2),空间复杂度为O(1),不能修改原数组中的值。 思路 emmm看讨论版的,英语可以的话建议直接看讨论版高票答案以及高票回复。 这个题目很特殊的一点是,数组中每个元素都在1...

EricJeffrey的博客 165

[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, f

coder 进阶的专栏 2829
上一篇: Leetcode NO.74 Search a 2D Matrix & binary search
下一篇: Leetcode NO.206 Reverse Linked List 反转链表(迭代&递归)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值