当前位置:网站首页>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;
}
}
}
边栏推荐
- Explore the authentication mechanism of StarUML
- 实例006:斐波那契数列
- Bluebridge cup internet of things competition basic graphic tutorial - clock selection
- 2020-05-21
- Typical low code apaas manufacturer cases
- Lori remote control commissioning record
- Weidongshan Internet of things learning lesson 1
- 猜谜语啦(7)
- Business modeling | process of software model
- Some pitfalls of win10 network sharing
猜你喜欢

Classic application of MOS transistor circuit design (2) - switch circuit design

剑指 Offer 06. 从尾到头打印链表

【三层架构】

STM32 lights up the 1.8-inch screen under Arduino IDE

OC and OD gate circuit

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

Arduino+a4988 control stepper motor

如何写Cover Letter?

STM32 single chip microcomputer - bit band operation

How to write cover letter?
随机推荐
Lori remote control LEGO motor
Negative pressure generation of buck-boost circuit
Example 007: copy data from one list to another list.
Detailed summary of FIO test hard disk performance parameters and examples (with source code)
Lori remote control commissioning record
Apaas platform of TOP10 abroad
猜谜语啦(9)
[paper reading] the latest transfer ability in deep learning: a survey in 2022
Bluebridge cup internet of things basic graphic tutorial - GPIO input key control LD5 on and off
Arduino operation stm32
TypeScript手把手教程,简单易懂
Several important parameters of LDO circuit design and type selection
STM32 single chip microcomputer - bit band operation
QEMU STM32 vscode debugging environment configuration
What are the test items of power battery ul2580
Guess riddles (142)
猜谜语啦(4)
Synchronization of QT multithreading
Classification of plastic surgery: short in long long long
Arduino+a4988 control stepper motor