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

[Leetcode] LC 287 Find the Duplicate Number 二分查找的方法看不懂

全局:

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

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

x
本帖最后由 南方Giraffe 于 2016-8-15 22:39 编辑

题目是:
https://leetcode.com/problems/find-the-duplicate-number/
public int findDuplicate(int[] nums) {            int low = 1, high = nums.length - 1;   
    while (low <= high) {        
        int mid = (int) (low + (high - low) * 0.5);        
        int cnt = 0;        
        for (int a : nums) {            
            if (a <= mid)
           ++cnt;        
        }        
        if (cnt <= mid)
            low = mid + 1;        
        else high = mid - 1;   
    }   
    return low;
}
这个答案看不懂,没有任何头绪,求指点



上一篇:leetcode 108, 109
下一篇:Leetcode 366 有没有好的BFS算法?
全局:
public int findDuplicate(int[] nums) {  
           int low = 1, high = nums.length - 1;   
    while (low < high) {    // this should be low < high, so when low = high, the index is the target we want.   
        int mid = (int) (low + (high - low) * 0.5);        
        int cnt = 0;        
        for (int a : nums) {            
            if (a <= mid) // count the number of elements not larger than mid.
           ++cnt;        
        }        
        if (cnt <= mid) // if the number no larger than mid, there are no duplicates for the number not larger than mid
        // since if there is, for the number larger than mid, there must also be a duplicated number(or else it cannot have enough element)
        // which breaks the rule for only one duplicated number.
            low = mid + 1;        
        else high = mid;   // for the opposite, since there must be a duplicate for the number no larger than mid, the number larger than mid shall be excluded.
        // it is impossible that the duplicated number is larger than n.
        // I think it is rather "high = mid" since the mid may also be the possible target.
    }   
    return low;
}

评分

参与人数 2大米 +6 收起 理由
anhenaiguo + 1 给你点个赞!
南方Giraffe + 5 很有用的信息!

查看全部评分

回复

使用道具 举报

推荐
indpoor 2016-8-16 11:00:18 | 只看该作者
全局:
CNT代表的是小于等于mid的数的个数,如果cnt < mid表明重复的数不再[low, mid]之间, 自然 low = mid +1,剩下的分析类似。另外注意看题目,如果数组的长度是n,能选得数只能实在【1,n-1】之间,且重复的数只有一个,这道题的bs是基于这两个特定条件的。

评分

参与人数 1大米 +5 收起 理由
南方Giraffe + 5 很有用的信息!

查看全部评分

回复

使用道具 举报

推荐
 楼主| 南方Giraffe 2016-9-14 21:35:49 | 只看该作者
全局:
susie_91 发表于 2016-9-14 14:58
LZLZ还在吗,我看你usc申了3个program,EE, DTIN, CSES,其中dtin和cses是???

前面那个好像叫data informatics,后面那个就是Computer Science (Engineer & Scientist),我们俗称的转专业37学分项目
回复

使用道具 举报

🔗
 楼主| 南方Giraffe 2016-8-15 22:36:47 | 只看该作者
全局:
抱歉,直接粘贴过来的答案没有排好版,这是答案的链接
https://discuss.leetcode.com/top ... using-binary-search
回复

使用道具 举报

🔗
 楼主| 南方Giraffe 2016-8-17 15:53:07 | 只看该作者
全局:
indpoor 发表于 2016-8-16 11:00
CNT代表的是小于等于mid的数的个数,如果cnt < mid表明重复的数不再[low, mid]之间, 自然 low = mid +1,剩 ...

明白了,之前把low跟high错认为是index了,感谢
回复

使用道具 举报

🔗
LordSavesU 2016-9-14 14:58:04 | 只看该作者
全局:
LZLZ还在吗,我看你usc申了3个program,EE, DTIN, CSES,其中dtin和cses是???
回复

使用道具 举报

🔗
LordSavesU 2016-9-15 13:06:54 | 只看该作者
全局:
南方Giraffe 发表于 2016-9-14 21:35
前面那个好像叫data informatics,后面那个就是Computer Science (Engineer &amp; Scientist),我们俗称的转 ...

好哒 谢谢 di还收转专业的啊!很少报这个转专业的录取哎地里
回复

使用道具 举报

🔗
Eric_Wenyi 2016-10-6 06:47:40 | 只看该作者
全局:
楼主楼主,最后你饭大和CMU-SV spring选了哪一个呀
回复

使用道具 举报

🔗
 楼主| 南方Giraffe 2016-10-6 07:11:01 | 只看该作者
全局:
Eric_Wenyi 发表于 2016-10-6 06:47
楼主楼主,最后你饭大和CMU-SV spring选了哪一个呀

最后秋季去了NYU-Courant 然后准备转去SV
回复

使用道具 举报

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

本版积分规则

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