当前位置:网站首页>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;
}
边栏推荐
- Idea packages jar packages and runs jar package commands
- 4.调整数组顺序使奇数位于偶数前面
- SQL Server 2016 learning records - View
- 漏洞分析丨HEVD-0x8.IntegerOverflow[win7x86]
- Go json.Decoder Considered Harmful
- gcc: error trying to exec 'as': execvp: No such file or directory
- 管道、管程、管态的区别
- 安装office自定义项 安装期间出错 解决办法
- 第一篇:UniAPP的小程序跨端开发-----创建uniapp项目
- 16. String inversion
猜你喜欢

数据库安全 --- 创建登录名 用户+配置权限【笔记】

Install mysql5.7 under centos7

SQL Server 2016 学习记录 --- 数据更新

Match file names from file paths using regular expressions

Aqua Data Studio 18.5.0导出insert语句
![[application of stack] - infix expression to suffix expression](/img/c1/879716342f6dd5eaa8b79c752eca16.png)
[application of stack] - infix expression to suffix expression

ACM winter vacation training 5

Django celery redis send email asynchronously

Multithreading and high concurrency (III) -- source code analysis AQS principle

配置树莓派,过程和遇到问题
随机推荐
AP Autosar平台设计 3架构
SQL Server 2016学习记录 --- 单表查询
第一篇:UniAPP的小程序跨端开发-----创建uniapp项目
逆元&组合数&快速幂
印度计划禁用中国电信设备!真离得开华为、中兴?
Pl/sql server syntax explanation
在mt6735中添加新的开机logo与开\关机动画
Why does the cluster need root permission
[wechat applet] project practice - lottery application
MySQL的SQL TRACE一例
发力大核、独显!英众科技2020十代酷睿独显产品发布
ogg里用多个filter语法应该怎么写?
5. Dynamic programming -- Fibonacci series
QT generation Exe file and run without QT environment (enigma virtual box for green executable software packaging) graphic tutorial
uni-app项目目录、文件作用介绍 及 开发规范
SQL Server 2016 learning record - Data Definition
SQL Server 2016 学习记录 ---视图
Use of Ogg parameter filter [urgent]
机器学习--手写英文字母2--导入与处理数据
8. Numbers that appear more than half of the time in the array