LintCode 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.

注意事项

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(n^2).
There is only one duplicate number in the array, but it could be repeated more than once.

样例
Given nums = [5,5,4,3,2,1] return 5
Given nums = [5,4,4,3,2,1] return 4

the First Solution:忽视掉注意事项,把数组排序,然后比较相邻元素,相同返回即可,在LintCode上也可以AC。
代码如下:

public class Solution {
    /*
     * @param nums: an array containing n + 1 integers which is between 1 and n
     * @return: the duplicate one
     */
    public int findDuplicate(int[] nums) {
        // write your code here
        Arrays.sort(nums);
        for(int i=0;i<nums.length-1;i++){
            if(nums[i]==nums[i+1]){
                return nums[i];
            }
        }
        return nums[0];
    }
}

the Second Solution:二分法。对于1到n的整数,例如1到10的整数,小于等于5的数,最少有5个(1,2,3,4,5),如果多于5个,那么一定是多了一个重复的数在1到5之间,那么下次就在1到5中间找,否则在6到10里面找(注意这里的二分法,不是对数组中的元素进行二分,而是对于1到n这n个数字进行二分查找)。
代码如下:

public class Solution {
    /*
     * @param nums: an array containing n + 1 integers which is between 1 and n
     * @return: the duplicate one
     */
    public int findDuplicate(int[] nums) {
        // write your code here
        int l=1,r=nums.length-1,mid;
        while(l<r){
            mid=l+(r-l)/2;
            int count=0;
            for(int x:nums){
                if(x<=mid){
                    count++;
                }
            }
            if(count>mid){
                r=mid;
            }
            else{
                l=mid+1;
            }
        }
        return l;
    }
}

the Third Solution: 参考资料
环检测问题,参考了上面链接的资料(有点没怎么理解,不过简单一点来说就是下面这样的),对于数组中的元素,对其做一个映射({1….n}到{1…n}的映射),f(nums[i])=A[ nums[i] ](i是数组下标),这样映射之后,相当于有了一个新的链表(我只是这么说,并没有构造一个链表),而且这个链表是带环的,因为有一个重复元素的存在。例如下面这个例子:
原数组nums: 5,4,3,4,2,1.
映射之后新的链表: 5,1,4,2,3,4,2,3,4,2,3………………其中4,2,3,4就形成了一个环。
那么问题到这,就可以参考之前写过的一篇文章,也是LintCode上另一道题,带环链表,就可以得到这道题的解法。
代码如下:

public class Solution {
    /*
     * @param nums: an array containing n + 1 integers which is between 1 and n
     * @return: the duplicate one
     */
    public int findDuplicate(int[] nums) {
        // write your code here
        int slow=0,fast=0;
        while(true){
            slow=nums[slow];
            fast=nums[nums[fast]];
            if(fast==slow){
                break;
            }
        }
        int p=0;
        while(true){
            slow=nums[slow];
            p=nums[p];
            if(slow==p){
                break;
            }
        }
        return p;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值