当前位置:网站首页>287. 寻找重复数-快慢指针
287. 寻找重复数-快慢指针
2022-07-05 08:31:00 【Mr Gao】
287. 寻找重复数
给定一个包含 n + 1 个整数的数组 nums ,其数字都在 [1, n] 范围内(包括 1 和 n),可知至少存在一个重复的整数。
假设 nums 只有 一个重复的整数 ,返回 这个重复的数 。
你设计的解决方案必须 不修改 数组 nums 且只用常量级 O(1) 的额外空间。
示例 1:
输入:nums = [1,3,4,2,2]
输出:2
示例 2:
输入:nums = [3,1,3,4,2]
输出:3
对于这题
首先,可以使用快慢指针找换
如果存在换,那么快慢指针会在环内相遇,但是相遇的点,不一定是换的起点
将快指针放回head,快慢指针以相同的步进移动,最终,会在环的入口相遇
你吹过我来时的风,我回到故乡,竟再次在原点相遇
int findDuplicate(int* nums, int numsSize) {
int fast = 0;
int slow = 0;
while (true) {
fast = nums[nums[fast]];
slow = nums[slow];
if (fast == slow) {
fast = 0;
while (fast != slow) {
fast = nums[fast];
slow = nums[slow];
}
return slow;
}
}
}
边栏推荐
- Simple design description of MIC circuit of ECM mobile phone
- On boost circuit
- [cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl
- [tutorial 15 of trio basic from introduction to proficiency] trio free serial communication
- Array integration initialization (C language)
- 实例006:斐波那契数列
- 实例004:这天第几天 输入某年某月某日,判断这一天是这一年的第几天?
- [paper reading] the latest transfer ability in deep learning: a survey in 2022
- Esp8266 interrupt configuration
- Synchronization of QT multithreading
猜你喜欢
MHA High available Cluster for MySQL
My-basic application 2: my-basic installation and operation
每日一题——输入一个日期,输出它是该年的第几天
Installation and use of libjpeg and ligpng
剑指 Offer 05. 替换空格
Management and use of DokuWiki (supplementary)
Lori remote control commissioning record
Daily question - input a date and output the day of the year
Briefly talk about the identification protocol of mobile port -bc1.2
【三层架构及JDBC总结】
随机推荐
每日一题——替换空格
Example 007: copy data from one list to another list.
STM32 single chip microcomputer - bit band operation
MATLAB小技巧(28)模糊綜合評價
Talk about the function of magnetic beads in circuits
【论文阅读】2022年最新迁移学习综述笔注(Transferability in Deep Learning: A Survey)
Soem EtherCAT source code analysis I (data type definition)
Sword finger offer 06 Print linked list from end to end
Bluebridge cup internet of things competition basic graphic tutorial - clock selection
Naming rules for FreeRTOS
Take you to understand the working principle of lithium battery protection board
[cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl
[tutorial 19 of trio basic from introduction to proficiency] detailed introduction of trio as a slave station connecting to the third-party bus (anybus PROFIBUS DP...)
[trio basic tutorial 16 from introduction to proficiency] UDP communication test supplement
Example 008: 99 multiplication table
UE pixel stream, come to a "diet pill"!
Several important parameters of LDO circuit design and type selection
Various types of questions judged by prime numbers within 100 (C language)
Detailed explanation of SQL server stored procedures
实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。