当前位置:网站首页>287. Looking for repeats - fast and slow pointer
287. Looking for repeats - fast and slow pointer
2022-07-05 08:35:00 【Mr Gao】
287. Look for repetitions
Given an inclusion n + 1 Array of integers nums , The numbers are all in [1, n] Within the scope of ( Include 1 and n), We know that there is at least one duplicate integer .
hypothesis nums Only A repeated integer , return The number of repetitions .
The solution you design must Don't modify Array nums And only constant level O(1) Extra space .
Example 1:
Input :nums = [1,3,4,2,2]
Output :2
Example 2:
Input :nums = [3,1,3,4,2]
Output :3
For this question
First , You can use the fast and slow pointer to find and change
If there is a change , Then the fast and slow pointers will meet in the ring , But the point of meeting , Not necessarily the starting point of change
Put the fast pointer back head, The speed pointer moves in the same step , Final , Will meet at the entrance of the ring
You blew the wind when I came , I returned to my hometown , Unexpectedly, we met again at the origin
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;
}
}
}
边栏推荐
- Arduino operation stm32
- Array integration initialization (C language)
- Charge pump boost principle - this article will give you a simple understanding
- Stm32--- systick timer
- Arrangement of some library files
- Chapter 18 using work queue manager (1)
- STM32 summary (HAL Library) - DHT11 temperature sensor (intelligent safety assisted driving system)
- Classification of plastic surgery: short in long long long
- Let's briefly talk about the chips commonly used in mobile phones - OVP chips
- [paper reading] the latest transfer ability in deep learning: a survey in 2022
猜你喜欢
实例004:这天第几天 输入某年某月某日,判断这一天是这一年的第几天?
On boost circuit
STM32 virtualization environment of QEMU
Example 009: pause output for one second
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
Agile project management of project management
Arduino burning program and Arduino burning bootloader
FIO测试硬盘性能参数和实例详细总结(附源码)
猜谜语啦(11)
猜谜语啦(3)
随机推荐
NTC thermistor application - temperature measurement
Cinq détails de conception du régulateur de tension linéaire
Shell script
Business modeling | process of software model
Business modeling of software model | object modeling
Run menu analysis
Google sitemap files for rails Projects - Google sitemap files for rails projects
GEO数据库中搜索数据
Weidongshan Internet of things learning lesson 1
Keil use details -- magic wand
What are the test items of power battery ul2580
OC and OD gate circuit
Affected tree (tree DP)
Esp8266 interrupt configuration
猜谜语啦(9)
Several implementation schemes of anti reverse connection protection of positive and negative poles of power supply!
实例002:“个税计算” 企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.
C language data type replacement
Various types of questions judged by prime numbers within 100 (C language)
猜谜语啦(5)