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. [转] HTML5终极备忘大全(图片版+文字版)---张鑫旭

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=1544 一.前言兼图片 ...

  2. wampserver下升级php7

    1.下载php7   http://windows.php.net/download#php-7.0 选择 VC14 x86 Thread Safe  64位选X64 32位选X86 2.下载VC14 ...

  3. php随机生成福彩双色球号码

    发布:thebaby   来源:net     [大 中 小] 不瞒您说,俺也是个双色球爱好者,经常买,但迟迟没有中过一等奖,哈哈.这里为大家介绍用php随机生成福彩双色球号码的二种方法,供朋友们学习 ...

  4. TDirectory.GetLastAccessTime获取指定目录最后访问时间

    使用函数: System.IOUtils.TDirectory.GetLastAccessTime 函数定义: class function GetLastAccessTime(const Path: ...

  5. IT从业人员必看的十几个论坛

    IT方面的论坛太多了,有综合,有专业,有行业,在各个论坛里混了几年,体会颇深,以前是论坛哪里人多,往哪里去,新浪论坛,网易是经常去的,人多啊,好几十万,去了以后才发现没有意思,没有共同的语言,于是逛专 ...

  6. POJ 2406 Power Strings 1961的简化版,kmp的next数组的应用

    题目: http://poj.org/problem?id=2406 跟1961差不多,题解就不写了,一开始理解错题了,导致WA一次. #include <stdio.h> #includ ...

  7. POJ 3687 Labeling Balls 逆向建图,拓扑排序

    题目链接: http://poj.org/problem?id=3687 要逆向建图,输入的时候要判重边,找入度为0的点的时候要从大到小循环,尽量让编号大的先入栈,输出的时候注意按编号的顺序输出重量, ...

  8. android SurfaceView绘制 重新学习--切图clipRect详解

    解释都在代码注释中: public class SampleView extends View { private Paint mPaint; private Path mPath; public S ...

  9. MySQL简单使用

    1.启动MySQL服务器实际上上篇已讲到如何启动MySQL.两种方法:一是用winmysqladmin,如果机器启动时已自动运行,则可直接进入下一步操作.二是在DOS方式下运行 d:/mysql/bi ...

  10. Java基础类型自动装箱(autoboxing)

    Java SE 1.5 版本及之后,开始提供自动装箱功能. 自动装箱,将基础类型“包装”成一种类型: 基本类型 -->  包装器类 如下过程可触发自动装箱过程: Integer count = ...