您的位置:首页 > 其它

Find the Duplicate Number

2016-05-13 21:47 423 查看
题目描述:

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.

这个题和之前的Missing Number很像。但是如果用排序法交换了数组顺序。不符合题目要求。

暴力循环法:

将数组中每一个数拿出来比较,有重复的找到了。但是时间复杂度超过了、

哈希表法:

遍历数组时,用一个集合记录已经遍历过的数,如果集合中已经有了说明是重复。但这样要空间,不符合。

二分法:

这个二分法和其他的不一样。用到抽屉原则。假如数组n=10,如果比5大的有6个,那么重复的数肯定比5大。这样的时间复杂度是O(NlogN)。符合题意要求。

代码如下:

public int findDuplicate(int[] nums) {
int min=0,max=nums.length-1;
while(min<max){
int mid=min+(max-min)/2;
int sum=count(nums,mid);
if(sum>mid)
max=mid;
else
min=mid+1;
}
return min;
}
public int count(int[] nums, int mid) {
int sum=0;
for (int i = 0; i < nums.length; i++) {
sum+=nums[i]<=mid?1:0;
}
return sum;
}


还有一种比较新颖的方法,用快慢指针的方法

假设数组中没有重复,那我们可以做到这么一点,就是将数组的下标和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->…,而环的起点就是重复的数。

代码如下:

public int findDuplicate(int[] nums) {
int slow = 0;
int fast = 0;
// 找到快慢指针相遇的地方
do{
slow = nums[slow];
fast = nums[nums[fast]];
} while(slow != fast);
int find = 0;
// 用一个新指针从头开始,直到和慢指针相遇
while(find != slow){
slow = nums[slow];
find = nums[find];
}
return find;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: