当前位置:网站首页>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;
}
}
}
边栏推荐
- Infected Tree(树形dp)
- 287. 寻找重复数-快慢指针
- U8g2 drawing
- [NAS1](2021CVPR)AttentiveNAS: Improving Neural Architecture Search via Attentive Sampling (未完)
- 猜谜语啦(9)
- Arrangement of some library files
- Arduino burning program and Arduino burning bootloader
- STM32 single chip microcomputer - bit band operation
- Speech recognition learning summary
- NTC thermistor application - temperature measurement
猜你喜欢

Arduino burning program and Arduino burning bootloader

Working principle and type selection of common mode inductor

Simple design description of MIC circuit of ECM mobile phone

STM32 single chip microcomputer - bit band operation

NTC thermistor application - temperature measurement

MATLAB小技巧(28)模糊综合评价

Guess riddles (2)

QEMU STM32 vscode debugging environment configuration

Example 009: pause output for one second

STM32 --- NVIC interrupt
随机推荐
Esp8266 interrupt configuration
Arduino+a4988 control stepper motor
TypeScript手把手教程,简单易懂
MySQL MHA high availability cluster
Classic application of MOS transistor circuit design (1) -iic bidirectional level shift
实例009:暂停一秒输出
Five design details of linear regulator
剑指 Offer 05. 替换空格
Example 010: time to show
Array integration initialization (C language)
Run菜单解析
Example 003: a complete square is an integer. It is a complete square after adding 100, and it is a complete square after adding 168. What is the number?
How apaas is applied in different organizational structures
2022.7.4-----leetcode.1200
Bluebridge cup internet of things competition basic graphic tutorial - clock selection
图解八道经典指针笔试题
STM32 --- NVIC interrupt
Search data in geo database
Agile project management of project management
STM32---ADC