当前位置:网站首页>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;
}
}
}
边栏推荐
- Several important parameters of LDO circuit design and type selection
- Is the security account given by Yixue school safe? Where can I open an account
- Matlab tips (28) fuzzy comprehensive evaluation
- Void* C is a carrier for realizing polymorphism
- Detailed summary of FIO test hard disk performance parameters and examples (with source code)
- MySQL之MHA高可用集群
- Anonymous structure in C language
- STM32 --- configuration of external interrupt
- Detailed explanation of SQL server stored procedures
- [cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl
猜你喜欢

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

Explain task scheduling based on Cortex-M3 in detail (Part 1)

MySQL MHA high availability cluster

Example 007: copy data from one list to another list.

More than 90% of hardware engineers will encounter problems when MOS tubes are burned out!

Example 010: time to show

【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl

【三层架构及JDBC总结】

Matlab tips (28) fuzzy comprehensive evaluation

Soem EtherCAT source code analysis attachment 1 (establishment of communication operation environment)
随机推荐
My-basic application 1: introduction to my-basic parser
UE像素流,来颗“减肥药”吧!
Semiconductor devices (I) PN junction
Chapter 18 using work queue manager (1)
MHA High available Cluster for MySQL
leetcode - 445. 两数相加 II
關於線性穩壓器的五個設計細節
【三层架构】
Sizeof (function name) =?
List of linked lists
Google sitemap files for rails Projects - Google sitemap files for rails projects
MySQL之MHA高可用集群
[trio basic tutorial 16 from introduction to proficiency] UDP communication test supplement
Example 004: for the day of the day, enter a day of a month of a year to judge the day of the year?
[trio basic tutorial 18 from introduction to proficiency] trio motion controller UDP fast exchange data communication
Stm32--- systick timer
STM32 lights up the 1.8-inch screen under Arduino IDE
Arduino+a4988 control stepper motor
实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
Arduino uses nrf24l01+ communication