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

问题:给定一个长度为 n+1 的数组,里面只有 1...n 的数值。可见必然有一个元素的值是重复出现的。假设只有最多只有一个元素的值会重复出现,求出重复的那个值。

特别要求:

  1. 不能修改原数组

  2. 只用固定空间,空间使用为 O(1)

  3. 时间复杂度要小于 O(n*2)

基于前两个要求,我首先想到的是两个指针,逐个遍历求解,但是耗时 O(n*n),超时了。在网上看到其他算法,能满足题目要求。

设 l 为左值, r 为右值, mid 为两者的中间值。值得注意的是,这里的 l, r, mid 均是指元素的值,不是指下标,之所以可以这么做,是因为题目的条件“ n+1 长的数组里面只有 1...n 的数值”。

  • 将数组扫一遍,得到大于等于 l 且 小于等于 mid 的元素个数,即为 num_l_mid。
  • 当 num_l_mid 大于 本应该有的个数,则将重复值定位在 [l, mid] 之间,缩小范围。
  • 当 num_l_mid 小于等于 本应该有的个数,则将重复值定位在 [mid, r] 之间,缩小范围。
  • 重复前面步骤,直到找到重复值。
int findDuplicate(vector<int>& nums) {

    if (nums.size() == ) {
return ;
} int l = ;
int r = (int)nums.size()-;
int mid = (r + l) / ; int cnt = ;
while (l < r) { int leftLen = mid - l + ; for (int i = ; i < nums.size(); i++) {
if ( l <= nums[i] && nums[i] <= mid) {
cnt++;
}
}if (l+ == r) {
if (cnt > leftLen) {
return l;
}else{
return r;
}
} if (cnt > leftLen) {
r = mid;
mid = (l + mid) / ;
}else{
l = mid;
mid = (mid + r) / ;
} mid = (r + l) / ;
cnt = ;
} return ;
}

[LeetCode] 287. Find the Duplicate Number 解题思路的更多相关文章

  1. LeetCode 287. Find the Duplicate Number (python 判断环,时间复杂度O(n))

    LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...

  2. 【LeetCode】287. Find the Duplicate Number 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存已经访问过的数字 链表成环 二分查找 日期 题目 ...

  3. [LeetCode] 287. Find the Duplicate Number 寻找重复数

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

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

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  5. [LeetCode] 287. Find the Duplicate Number(Floyd判圈算法)

    传送门 Description Given an array nums containing n + 1 integers where each integer is between 1 and n  ...

  6. LeetCode : 287. Find the Duplicate Number

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAACRAAAAMMCAYAAAAhQhmZAAAMFGlDQ1BJQ0MgUHJvZmlsZQAASImVlw ...

  7. leetcode 217. Contains Duplicate 287. Find the Duplicate Number 442. Find All Duplicates in an Array 448. Find All Numbers Disappeared in an Array

    后面3个题都是限制在1-n的,所有可以不先排序,可以利用巧方法做.最后两个题几乎一模一样. 217. Contains Duplicate class Solution { public: bool ...

  8. 287. Find the Duplicate Number hard

    287. Find the Duplicate Number   hard http://www.cnblogs.com/grandyang/p/4843654.html 51. N-Queens h ...

  9. 【LeetCode】652. Find Duplicate Subtrees 解题报告(Python)

    [LeetCode]652. Find Duplicate Subtrees 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

随机推荐

  1. As3 里的正则相关

    用正则的时候 不要用if(content.match("test").length > 0) ...; 改成 if(content.match(/test/g).length ...

  2. Graphviz使用简介(中文乱码的问题)

    Graphviz使用简介 graphviz是基于dot语言的绘图工具,可以画有向图.无向图.关系图.目录图.流程图等.具体作用可见它的官方网站 一些参考的网址: http://www.open-ope ...

  3. Linux Shell脚本之自动修改IP

    作为一名Linux SA,日常运维中很多地方都会用到脚本,而服务器的ip一般采用静态ip或者MAC绑定,当然后者比较操作起来相对繁琐,而前者我们可以设置主机名.ip信息.网关等配置.修改成特定的主机名 ...

  4. Shell数组

    #!/bin/bash#数组的使用#数组赋值方式:#1. user[index]=value index为0,1,2,3,4....数组下标值#2. user=(value0 value1 value ...

  5. 数据库创建&数据表创建

    --第2_1题创建数据库 create database Student201038897041 on primary (name='student1', filename='F:\coures\SQ ...

  6. thinkphp多表查询

    在学习thinkphp 的过程中,需要对多表进行操作,但是在实际过程中,总是遇到各种问题,所以写下这篇博文,作为自己的学习历程 在操作过程中,两表查询都没有问题,但是三表查询就开始出现问题 有以下三张 ...

  7. git push用法和常见问题分析

    在使用git 处理对android的修改的过程之中总结的.但不完善 Git push $ git push origin test:master         // 提交本地test分支作为远程的m ...

  8. Js处理数据——前端分页工具

    这几天有小伙伴讨论起了分页的相关问题,这里我也简单讲下前端如何简单便捷的利用Js(库)写出优雅,好用的分页工具. 分页是个很简单又超多接触的技术点,粗略来讲分如下两种: 真分页--每次根据页码.页大小 ...

  9. C#在声明对象时对其赋值的一种方式

    今天学会一种更方便的赋值方式,如下, 同时存档一个通过 打开对话框 获取地址的方式. private string GetSaveAsPathXls(string defaultFileName) { ...

  10. Tmux:终端复用器

    转自Tmux:终端复用器 Tmux 是一个 C 语言编写的终端,它能够在单一窗口中同时访问和控制多个终端.它是一个类似于GNU Screen 的工具.使用它,用户可以在 Linux 系统上管理多个任务 ...