LeetCode 287. Find the Duplicate Number(找重复数字)

28 篇文章 0 订阅
23 篇文章 0 订阅

原题网址:https://leetcode.com/problems/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:

  1. You must not modify the array (assume the array is read only).
  2. You must use only constant, O(1) extra space.
  3. Your runtime complexity should be less than O(n2).
  4. There is only one duplicate number in the array, but it could be repeated more than once.

方法一:应用鸽笼原理进行二分法查找。

因为在1~n的范围内最多只能放n个不同的数字,但现在数组放了n+1个数字,所以至少有一个重复。

对于在1~n范围内的某个数字m,那么数组中小于等于m的数字最少有m个,并且刚好为m的时候,1~m之间不会有重复,为什么呢?我们分开来证明。


public class Solution {
    public int findDuplicate(int[] nums) {
        int low = 1, high = nums.length-1;
        while (low<high) {
            int mid = (low+high)/2;
            int count = 0;
            for(int num: nums) if (num<=mid) count++;
            if (count > mid) high = mid; else low = mid + 1;
        }
        return low;
    }
}

方法二:循环链条检测法。这个方法我没有想出来,网上找到的,使用的技术竟然和循环链表检测的方法一模一样!


public class Solution {
    public int findDuplicate(int[] nums) {
        int slow = 0, fast = 0;
        do {
            slow = nums[slow];
            fast = nums[nums[fast]];
        } while (nums[slow] != nums[fast]);
        int restart = 0;
        while (nums[restart] != nums[slow]) {
            restart = nums[restart];
            slow = nums[slow];
        }
        return nums[restart];
    }
}
参考文章:

http://bookshadow.com/weblog/2015/09/28/leetcode-find-duplicate-number/
https://segmentfault.com/a/1190000003817671

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值