当前位置:网站首页>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;
}
}
}
边栏推荐
- Explain task scheduling based on Cortex-M3 in detail (Part 2)
- Example 002: the bonus paid by the "individual income tax calculation" enterprise is based on the profit commission. When the profit (I) is less than or equal to 100000 yuan, the bonus can be increase
- Use indent to format code
- Sql Server的存儲過程詳解
- On boost circuit
- [trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code
- Keil use details -- magic wand
- Briefly talk about the identification protocol of mobile port -bc1.2
- Arrangement of some library files
- 2022.7.4-----leetcode. one thousand and two hundred
猜你喜欢
STM32 --- NVIC interrupt
Charge pump boost principle - this article will give you a simple understanding
NTC thermistor application - temperature measurement
STM32 --- serial port communication
Keil use details -- magic wand
STM32 single chip microcomputer - bit band operation
More than 90% of hardware engineers will encounter problems when MOS tubes are burned out!
List of linked lists
【三层架构】
Example 005: three numbers sorting input three integers x, y, Z, please output these three numbers from small to large.
随机推荐
如何写Cover Letter?
Example 009: pause output for one second
QEMU STM32 vscode debugging environment configuration
Let's briefly talk about the chips commonly used in mobile phones - OVP chips
Esphone retrofits old fans
Detailed explanation of SQL server stored procedures
Array integration initialization (C language)
Soem EtherCAT source code analysis II (list of known configuration information)
[paper reading] the latest transfer ability in deep learning: a survey in 2022
Lori remote control commissioning record
UE pixel stream, come to a "diet pill"!
[NAS1](2021CVPR)AttentiveNAS: Improving Neural Architecture Search via Attentive Sampling (未完)
Negative pressure generation of buck-boost circuit
L298N module use
实例001:数字组合 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
Arrangement of some library files
动力电池UL2580测试项目包括哪些
Bluebridge cup internet of things basic graphic tutorial - GPIO output control LD5 on and off
Installation and use of libjpeg and ligpng
List of linked lists