Find the Duplicate Number
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.
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.
From: https://segmentfault.com/a/1190000003817671
二分法
复杂度
时间 O(NlogN) 空间 O(1)
思路
实际上,我们可以根据抽屉原理简化刚才的暴力法。我们不一定要依次选择数,然后看是否有这个数的重复数,我们可以用二分法先选取n/2,按照抽屉原理,整个数组中如果小于等于n/2的数的数量大于n/2,说明1到n/2这个区间是肯定有重复数字的。比如6个抽屉,如果有7个袜子要放到抽屉里,那肯定有一个抽屉至少两个袜子。这里抽屉就是1到n/2的每一个数,而袜子就是整个数组中小于等于n/2的那些数。这样我们就能知道下次选择的数的范围,如果1到n/2区间内肯定有重复数字,则下次在1到n/2范围内找,否则在n/2到n范围内找。下次找的时候,还是找一半。
注意
我们比较的
mid
而不是nums[mid]
因为mid是下标,所以判断式应为
cnt > mid
,最后返回min
public class Solution {
public int findDuplicate(int[] nums) {
int min = , max = nums.length - ;
while (min <= max) {
// 找到中间那个数
int mid = min + (max - min) / ;
int cnt = ;
// 计算总数组中有多少个数小于等于中间数
for (int i = ; i < nums.length; i++) {
if (nums[i] <= mid) {
cnt++;
}
}
// 如果小于等于中间数的数量大于中间数,说明前半部分必有重复
if (cnt > mid) {
max = mid - ;
// 否则后半部分必有重复
} else {
min = mid + ;
}
}
return min;
}
}
映射找环法
复杂度
时间 O(N) 空间 O(1)
思路
假设数组中没有重复,那我们可以做到这么一点,就是将数组的下标和1到n每一个数一对一的映射起来。比如数组是213
,则映射关系为0->2, 1->1, 2->3
。假设这个一对一映射关系是一个函数f(n),其中n是下标,f(n)是映射到的数。如果我们从下标为0出发,根据这个函数计算出一个值,以这个值为新的下标,再用这个函数计算,以此类推,直到下标超界。实际上可以产生一个类似链表一样的序列。比如在这个例子中有两个下标的序列,0->2->3
。
但如果有重复的话,这中间就会产生多对一的映射,比如数组2131
,则映射关系为0->2, {1,3}->1, 2->3
。这样,我们推演的序列就一定会有环路了,这里下标的序列是0->2->3->1->1->1->1->...
,而环的起点就是重复的数。
所以该题实际上就是找环路起点的题,和Linked List Cycle II一样。我们先用快慢两个下标都从0开始,快下标每轮映射两次,慢下标每轮映射一次,直到两个下标再次相同。这时候保持慢下标位置不变,再用一个新的下标从0开始,这两个下标都继续每轮映射一次,当这两个下标相遇时,就是环的起点,也就是重复的数。对这个找环起点算法不懂的,请参考Floyd's Algorithm。
注意
第一次找快慢指针相遇用do-while循环
public class Solution {
public int findDuplicate(int[] nums) {
int slow = ;
int fast = ;
// 找到快慢指针相遇的地方
do{
slow = nums[slow];
fast = nums[nums[fast]];
} while(slow != fast);
int find = ;
// 用一个新指针从头开始,直到和慢指针相遇
while(find != slow){
slow = nums[slow];
find = nums[find];
}
return find;
}
}
Find the Duplicate Number的更多相关文章
- [LeetCode] Find the Duplicate Number 寻找重复数
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- 287. Find the Duplicate Number hard
287. Find the Duplicate Number hard http://www.cnblogs.com/grandyang/p/4843654.html 51. N-Queens h ...
- Leetcode Find the Duplicate Number
最容易想到的思路是新开一个长度为n的全零list p[1~n].依次从nums里读出数据,假设读出的是4, 就将p[4]从零改成1.如果发现已经是1了,那么这个4就已经出现过了,所以他就是重复的那个数 ...
- LeetCode——Find the Duplicate Number
Description: Given an array nums containing n + 1 integers where each integer is between 1 and n (in ...
- 287. Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- [LeetCode] 287. Find the Duplicate Number 解题思路
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- Find the Duplicate Number 解答
Question Given an array nums containing n + 1 integers where each integer is between 1 and n (inclus ...
- LeetCode 287. Find the Duplicate Number (找到重复的数字)
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- [Swift]LeetCode287. 寻找重复数 | Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
随机推荐
- php-fpm进程数优化方法
原文地址:https://www.douban.com/note/315222037/ 背景最近将Wordpress迁移至阿里云.由于自己的服务器是云服务器,硬盘和内存都比较小,所以内存经常不够使,通 ...
- base64 加密
Base64 参考网站:http://zh.wikipedia.org/wiki/Base64 简介 是网络上使用最广泛的编码系统,能够将任何二进制数据,转换成只有 65 个字符组成的文本文件 a~z ...
- Sql导出数据报错-->SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问
SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问,因为此组件已作为此服 ...
- 构建spring+mybatis+redis架构时遇到的小异常
异常一: Caused by: java.lang.NoSuchMethodError: org.springframework.beans.BeanUtils.instantiateClass(Lj ...
- AD域服务器|两台DC无法进行复制同步
注:本文由Colin撰写,版权所有!转载请注明原文地址,谢谢合作! 说明:前段时间公司两台域控出现了一些问题导致数据无法相互进行同步,DC之间也无法进行共享访问,网络用户无法通过计算机名映射的共享访问 ...
- 解决英文或数字在HTMl网页中不自动换行。
对于网页设计的新手而言,在接触一段时间的HTML/CSS后,一定会遇到这样的问题:对于已经定义了宽度的容器(如DIV,TD,段落等)如果里面出现了较长的英文或数字,则内容不能自动换行然后会将框架撑出设 ...
- 利用UIActivityController调用ios系统自带的分享功能,实现微信发布多图的功能
通过一番查找以后找到一个类UIActivityController,可以调用系统的social.framework中的分享接口.看下面的图就知道了,这个还是挺常见的 微信发布多图 借鉴了CSDN上的一 ...
- Linux 修改主机名 和 ip 映射关系
1. 修改主机名 vim /etc/sysconfig/network NETWORKING=yes HOSTNAME=hadoop 2. 修改主机名和IP的映射关系 vim /etc/hosts 1 ...
- Mac Pro 解压安装MySQL二进制分发版 mysql-5.6.30-osx10.11-x86_64.tar.gz(不是dmg的)
没有mac的root密码,当前用户有sudo权限,所以想以root身份执行的命令都加了sudo. 是否存在 _mysql 用户和用户组,并查看用户 _mysql 是不是用户组 _mysql 的成员. ...
- Android 实现简单音乐播放器(二)
在Android 实现简单音乐播放器(一)中,我介绍了MusicPlayer的页面设计. 现在,我简单总结一些功能实现过程中的要点和有趣的细节,结合MainActivity.java代码进行说明(写出 ...