当前位置:网站首页>287. Find the Duplicate Number
287. Find the Duplicate Number
2022-07-28 10:33:00 【Xiaoliu xuezha】
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.
Example 1:
Input: [1,3,4,2,2]
Output: 2
Example 2:
Input: [3,1,3,4,2]
Output: 3
Note:
- You must not modify the array (assume the array is read only).
- You must use only constant, O(1) extra space.
- Your runtime complexity should be less than O(n2).
- There is only one duplicate number in the array, but it could be repeated more than once.
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/find-the-duplicate-number
Ring detection algorithm
Using two pointers , One of them moves twice as fast as the other
When we met , You know where the ring starts
Take the array value as a pointer to the array index (1-n)
int findDuplicate(int* a, int l){
int x=a[0],y=a[0];
while(1)
{
x=a[x];
y=a[a[y]];
if(x==y)break;
}
int xx=a[0];
int yy=x;
while(xx!=yy)
{
xx=a[xx];
yy=a[yy];
}
return xx;
}
边栏推荐
- SQL Server 2016 学习记录 --- 集合查询
- 死锁算法:银行家算法和安全性算法
- C语言 二级指针详解及示例代码
- Alibaba cloud image address
- IDEA创建我的第一个项目
- SQL Server 2016 learning records - View
- uni-app项目目录、文件作用介绍 及 开发规范
- string matching
- QT generation Exe file and run without QT environment (enigma virtual box for green executable software packaging) graphic tutorial
- 字符串匹配
猜你喜欢

ACM winter vacation training 5

gcc: error trying to exec 'as': execvp: No such file or directory

14. Double pointer - the container that holds the most water

gcc: error trying to exec 'as': execvp: No such file or directory

机器学习--手写英文字母2--导入与处理数据

Record a parent-child project in idea, modify the name of project and module, and test it personally!

Install mysql5.7 under centos7

IDEA打包jar包及运行jar包命令

【微信小程序】项目实战—抽签应用

利用正则表达式从文件路径中匹配文件名
随机推荐
Ie compatibility problem handling
死锁算法:银行家算法和安全性算法
【微信小程序】项目实战—抽签应用
Netease written test No. 2 -- typical application of European distance
阿里云镜像地址
Deadlock algorithm: banker algorithm and security algorithm
SQL Server 2016 learning records - View
ZTE: 5nm 5g base station chip is being introduced!
Inverse element & combinatorial number & fast power
机器学习--手写英文字母2--导入与处理数据
2. Output one of the repeated numbers in the array
ogg里用多个filter语法应该怎么写?
Match file names from file paths using regular expressions
2021-10-13arx
8. Numbers that appear more than half of the time in the array
Read write separation standby backup error
10. The penultimate node in the linked list
C语言 输入带空格的字符串
Summary of key points of bank entry examination
AP Autosar平台设计 3架构