查看: 3585| 回复: 18
跳转到指定楼层
上一主题 下一主题
收起左侧

[数组] Leetcode287. Find the Duplicate Number Follow up

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
题目:

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.
如果没有限制修改数组,有没有什么其他的算法?



上一篇:请教一个二分法的题
下一篇:分享我刷Leetcode的题解(C++版本)
全局:
允许修改原数组的话,就用pigeonhole principle直接sort。
O(n) time, O(1) space

  1. int findDuplicate(vector<int>& nums) {

  2.         for(int i=0; i<nums.size(); i++) {
  3.             
  4.             int val = nums[i];
  5.             if(i!=val){
  6.                
  7.                 if(nums[val]==val)
  8.                     return val;
  9.                 swap(nums[val], nums[i]);
  10.                 i--;
  11.             }
  12.             
  13.         }
  14.         
  15.         return -1;
  16.     }
复制代码
回复

使用道具 举报

全局:
111180611 发表于 2016-6-1 02:01
为什么low从1开始?

从0开始应该也是对的。。因为不存在length小于2的情况。。
我的java代码这么写的,能过:
public int findDuplicate(int[] nums) {
        int start = 0;
        int end = nums.length-1;
        while (start<end){
            int mid = start+(end-start)/2;
            int count = 0;
            for (int num : nums){
                if (num<=mid) count++;
            }
            if (count>mid) end = mid;
            else start = mid+1;
        }
        return start;
    }
回复

使用道具 举报

推荐
JimmyZhuang 2016-5-31 08:36:14 | 只看该作者
全局:
本帖最后由 JimmyZhuang 于 2016-5-31 08:37 编辑

int findDuplicate(vector<int>& nums) {
   int low = 1, high = nums.size() - 1;
   while(low < high) {
      int mid = low + (high - low) / 2;
      int c = 0;
      for(int i = 0; i < nums.size(); i++) {
         if(nums <= mid) c++;
      }
      if(c <= mid)
         low = mid + 1;
      else
         high = mid;
   }
   return nums[low];
}
回复

使用道具 举报

🔗
blackrose 2016-5-31 10:55:33 | 只看该作者
全局:
hashmap, first sort then binary search
回复

使用道具 举报

🔗
blackrose 2016-5-31 10:55:59 | 只看该作者
全局:
JimmyZhuang 发表于 2016-5-31 08:36
int findDuplicate(vector& nums) {
   int low = 1, high = nums.size() - 1;
   while(low < high) {

you need to sort first
回复

使用道具 举报

🔗
JimmyZhuang 2016-5-31 14:04:20 | 只看该作者
全局:

No, I don't need to sort. Take a look at my low and high, they are not indexes.
回复

使用道具 举报

🔗
e453079612 2016-5-31 21:39:55 | 只看该作者
全局:
JimmyZhuang 发表于 2016-5-31 08:36
int findDuplicate(vector& nums) {
   int low = 1, high = nums.size() - 1;
   while(low < high) {

二楼最后返回的结果应该是low,不是nums[low]
回复

使用道具 举报

🔗
vae371 2016-5-31 22:37:34 | 只看该作者
全局:
请问一下,这个不是全都取异或看剩下来的数就行了吗?
回复

使用道具 举报

🔗
zpinthehouse 2016-5-31 23:46:46 | 只看该作者
全局:
vae371 发表于 2016-5-31 22:37
请问一下,这个不是全都取异或看剩下来的数就行了吗?

那是另外一道题。。这个重复可以重复很多次的。。
回复

使用道具 举报

🔗
zpinthehouse 2016-5-31 23:49:12 | 只看该作者
全局:
答案那个O(n)的做法是把这个看成linkedlist cycle...好难想。。还是binary search那个,判断这个数是否小于当前的mid,比较好想。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表