当前位置:网站首页>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;
}
边栏推荐
- select如果不加order by子句,返回结果的顺序是不可靠的
- 最短路专题
- 数据库安全 --- 创建登录名 用户+配置权限【笔记】
- Install MySQL under centos7, and the online articles are not accurate
- MySQL的SQL TRACE一例
- IE兼容性问题处理
- 管道、管程、管态的区别
- Summary of key points of bank entry examination
- SQL Server 2016 learning records - View
- Performance test of API gateway APIs IX in Google cloud T2a and T2D
猜你喜欢

SQL Server 2016 学习记录 --- 集合查询

SQL Server 2016 learning records - data update

CentOS7下安装mysql5.7

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

Performance test of API gateway APIs IX in Google cloud T2a and T2D

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

uni-app项目目录、文件作用介绍 及 开发规范

Typora使用教程

Typora tutorial

C language secondary pointer explanation and example code
随机推荐
It's settled! On July 30!
管道、管程、管态的区别
多线程与高并发(三)—— 源码解析 AQS 原理
Ie compatibility problem handling
13、哈希表——两个链表第一个公共节点
12. Double pointer -- merge two ordered linked lists
14、双指针——盛最多水的容器
发力大核、独显!英众科技2020十代酷睿独显产品发布
中兴通讯总裁徐子阳:5nm芯片将在2021年推出
(1)机器学习概念总结
15、判断二维数组中是否存在目标值
Pl/sql server syntax explanation
Summary of key points of bank entry examination
11. Linked list inversion
胡润发布2020中国芯片设计10强民营企业:华为海思竟然没有上榜!
3. Print the linked list in reverse order with the array
Match file names from file paths using regular expressions
DBeaver的操作日志
SuperMap iServer发布管理以及调用地图服务
12、双指针——合并两个有序链表