一个长度为 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. android中后一个activity传值给前一个activity的实现

    前一个activity跳转到后一个activity设置code: Intent intent=new Intent(MainActivity.this,ActivityTwo.class); star ...

  2. Meeting 加虚拟边

    Bessie and her friend Elsie decide to have a meeting. However, after Farmer John decorated his fence ...

  3. Ubuntu 16.04安装录屏软件SimpleScreenRecorder

    安装: sudo add-apt-repository ppa:maarten-baert/simplescreenrecorder sudo apt-get update sudo apt-get ...

  4. ArcGIS for Android离线数据编辑实现原理

    来自:http://blog.csdn.net/arcgis_mobile/article/details/7565877 ArcGIS for Android中现已经提供了离线缓存图片的加载功能,极 ...

  5. day4-hdfs的核心工作原理\写数据流程 \读数据流程

    namenode元数据管理要点 1.什么是元数据? hdfs的目录结构及每一个文件的块信息(块的id,块的副本数量,块的存放位置<datanode>) 2.元数据由谁负责管理? namen ...

  6. 怎样在Swift中使用NSError

    步骤一:声明NSError变量. 一定要加"?",不加或者加"!"都不行.由于使用了optional,所以要用var而不用let. var error: NSE ...

  7. 连接App.config

    ConfigurationManager.AppSettings["AdminName"]; 连接App.config的字符

  8. WPF中的常用布局 栈的实现 一个关于素数的神奇性质 C# defualt关键字默认值用法 接口通俗理解 C# Json序列化和反序列化 ASP.NET CORE系列【五】webapi整理以及RESTful风格化

    WPF中的常用布局   一 写在开头1.1 写在开头微软是一家伟大的公司.评价一门技术的好坏得看具体的需求,没有哪门技术是面面俱到地好,应该抛弃对微软和微软的技术的偏见. 1.2 本文内容本文主要内容 ...

  9. mac svn cornerstone 破解版资源以及使用方法(仅供学习,非商业使用)

    mac svn 可视化客户端,找了好久,不知道是我搜索的有问题还是怎么了,没有特别好用的. 后来发现了一个大神做的破解版的 cornerstone,具体大神的博客我给忘记了,后续找到会贴出地址,以供膜 ...

  10. JSON入门指南

    JSON 即 JavaScript Object Natation,它是一种轻量级的数据交换格式,很适合于server与 JavaScript 的交互.本文将高速解说 JSON 格式.并通过代码演示样 ...