当前位置:网站首页>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;
}
}
}
边栏推荐
- STM32---IIC
- 如何写Cover Letter?
- 实例006:斐波那契数列
- STM32 virtualization environment of QEMU
- Ble encryption details
- Sword finger offer 06 Print linked list from end to end
- Stablq of linked list
- Take you to understand the working principle of lithium battery protection board
- Esphone Feixun DC1 soft change access homeassstant
- [nas1] (2021cvpr) attentivenas: improving neural architecture search via attentive sampling (unfinished)
猜你喜欢
STM32 --- NVIC interrupt
Ble encryption details
[NAS1](2021CVPR)AttentiveNAS: Improving Neural Architecture Search via Attentive Sampling (未完)
实例008:九九乘法表
实例010:给人看的时间
实例007:copy 将一个列表的数据复制到另一个列表中。
leetcode - 445. Add two numbers II
Arduino+a4988 control stepper motor
Stm32--- systick timer
STM32 single chip microcomputer -- volatile keyword
随机推荐
[NAS1](2021CVPR)AttentiveNAS: Improving Neural Architecture Search via Attentive Sampling (未完)
[nas1] (2021cvpr) attentivenas: improving neural architecture search via attentive sampling (unfinished)
关于线性稳压器的五个设计细节
Installation and use of libjpeg and ligpng
[trio basic from introduction to mastery tutorial XIV] trio realizes unit axis multi-color code capture
Soem EtherCAT source code analysis I (data type definition)
Semiconductor devices (III) FET
Several implementation schemes of anti reverse connection protection of positive and negative poles of power supply!
实例010:给人看的时间
Is the security account given by Yixue school safe? Where can I open an account
[paper reading] the latest transfer ability in deep learning: a survey in 2022
剑指 Offer 09. 用两个栈实现队列
亿学学堂给的证券账户安不安全?哪里可以开户
剑指 Offer 06. 从尾到头打印链表
STM32 single chip microcomputer -- volatile keyword
Explication de la procédure stockée pour SQL Server
Take you to understand the working principle of lithium battery protection board
MATLAB小技巧(28)模糊综合评价
Example 004: for the day of the day, enter a day of a month of a year to judge the day of the year?
Semiconductor devices (I) PN junction