一个长度为 n + 1 的整形数组,其中的数字都在 1 到 n 之间,包括 1 和 n ,可知至少有一个重复的数字存在。假设只有一个数字重复,找出这个重复的数字。
注意:
    不能更改数组内容(假设数组是只读的)。
    只能使用恒定的额外空间,即要求空间复杂度是 O(1) 。
    时间复杂度小于 O(n2)
    数组中只有一个数字重复,但它可能不止一次重复出现。
详见:https://leetcode.com/problems/find-the-duplicate-number/description/

方法一:

class Solution {
public:
int findDuplicate(vector<int>& nums) {
int left=1,right=nums.size()-1;
while(left<right)
{
int mid=left+(right-left)/2;
int cnt=0;
for(int val:nums)
{
if(val<=mid)
{
++cnt;
}
}
if(cnt<=mid)
{
left=mid+1;
}
else
{
right=mid;
}
}
return left;
}
};

方法二:

class Solution {
public:
int findDuplicate(vector<int>& nums) {
int s=0,f=0,t=0;
while(true)
{
s=nums[s];
f=nums[nums[f]];
if(s==f)
{
break;
}
}
while(true)
{
s=nums[s];
t=nums[t];
if(s==t)
{
break;
}
}
return s;
}
};

参考:https://www.cnblogs.com/grandyang/p/4843654.html

287 Find the Duplicate Number 寻找重复数的更多相关文章

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

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

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

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

  3. 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 ...

  4. 287. Find the Duplicate Number hard

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

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

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

  6. 287. Find the Duplicate Number

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

  7. [LeetCode] 287. Find the Duplicate Number 解题思路

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

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

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

  9. 287. Find the Duplicate Number 找出数组中的重复数字

    [抄题]: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive ...

随机推荐

  1. scp远程文件传输

    第一次.提示下载公钥 [root@rhel5 ~]# scp install.log root@192.168.124.129:/tmp The authenticity of host '192.1 ...

  2. P1765 手机_NOI导刊2010普及(10)

    P1765 手机_NOI导刊2010普及(10) 题目描述 一般的手机的键盘是这样的: 1 2 abc 3 def 4 ghi 5 jkl 6 mno 7 pqrs 8 tuv 9 wxyz * 0 ...

  3. 关于python内存管理里的引用计数算法和标记-清楚算法的讨论

    先记录于此,后续有时间再深究吧: 1.https://www.zhihu.com/question/33529443 2.http://patshaughnessy.net/2013/10/30/ge ...

  4. [大雾雾雾雾] 告别该死的 EFCore Fluent API

    [EF Core Oracle  列名大小写问题] [EF Core Oracle column name case problem] [EF Core PostgreSql 列名大小写问题] [EF ...

  5. python异步回调函数的实现

    #coding:utf-8 from socket import * import time #简单的服务器程序 监听用户连接,接收用户发来的信息,并返回反馈 def main(): HOST = & ...

  6. FTP Server完整篇 ubuntu 10.04

    1. sudo apt-get install vsftpd   #安裝FTP Server(vsftp:very secure FTP) 安装后,会自动生成ftp用户,和ftp的文件夹,如果没有自动 ...

  7. 【项目实战】---使用ajax完毕username是否存在异步校验

    小伙伴在上网的时候.须要下载或者观看某些视频资料,更或者是在逛淘宝的时候.我们都须要注冊一个用户,当我们填写好各种信息,点击确定的时候.提示username已经存在.小编就想,为什么当我们填写完use ...

  8. Mariadb 事务

    事务 事务具有ACID特性:原子性(A,atomicity).一致性(C,consistency).隔离性(I,isolation).持久性(D,durabulity). 1.原子性:事务内的所有操作 ...

  9. php &amp; 和 &amp;amp; (主要是url 问题)

    ini_set('arg_separator.output','&'); 也能够在php.ini 改动

  10. ppt五种经典字体组合

    在做ppt中常常为使用哪种字体而头疼,如今将ppt的经典字体附上.希望对大家有帮助 五种经典的字体组合 标题字体 正文字体 使用场合 方正综艺简体 微软雅黑 课题汇报.咨询报告.学术研讨等正式场合 方 ...