当前位置:网站首页>287. Find the Duplicate Number
287. Find the Duplicate Number
2022-07-28 10:15:00 【小柳学渣】
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.
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/find-the-duplicate-number
环检测算法
利用两个指针,其中一个移动比另一个快一倍
相遇时,就知道环开始的地方
把数组值当作指向数组索引的指针(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;
}
边栏推荐
- Small knowledge in Oracle
- (1) Summary of machine learning concepts
- It's settled! On July 30!
- DBeaver的操作日志
- SQL Server 2016 学习记录 --- 嵌套查询
- 华为入股石墨烯材料厂商富烯科技,持股10%
- Multithreading and high concurrency (III) -- source code analysis AQS principle
- SQL Server 2016 学习记录 --- 数据更新
- Install MySQL under centos7, and the online articles are not accurate
- jvm原理
猜你喜欢

利用正则表达式从文件路径中匹配文件名

SQL Server 2016 学习记录 --- 数据定义

SQL Server 2016学习记录 --- 连接查询

Aqua Data Studio 18.5.0 export insert statement

初识SuperMap iDesktop

Typora tutorial

IDEA创建我的第一个项目

SuperMap iserver publishing management and calling map services

ACM寒假集训#5

Voice chat app - how to standardize the development process?
随机推荐
(1)机器学习概念总结
SQL Server 2016 learning records - View
Xu Ziyang, President of ZTE: 5nm chip will be launched in 2021
Massive data topn problem
Talk about the problem of preventing others from debugging websites through console based on JS implementation
数据库安全 --- 创建登录名 用户+配置权限【笔记】
7、二分法——寻找一组重复或者有序但是旋转的数组
SQL Server 2016学习记录 --- 单表查询
Problem summary file
10 minute quick start EVs [play Huawei cloud]
2. 输出数组中重复的数字之一
Use of Ogg parameter filter [urgent]
漏洞分析丨HEVD-0x8.IntegerOverflow[win7x86]
用K-means聚类分类不同行业的关税模型
3. Print the linked list in reverse order with the array
5、动态规划---斐波那契数列
gcc: error trying to exec 'as': execvp: No such file or directory
SQL Server 2016 learning records - connection query
传全球半导体设备巨头或将于上海建合资工厂!
JVM principle