一个长度为 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. Layui导航、面包屑

    物不在多,有用则精! 学习使用链接 导航:导航一般指页面引导性频道集合,多以菜单的形式呈现,可应用于头部和侧边,是整个网页画龙点晴般的存在.面包屑结构简单,支持自定义分隔符.千万不要忘了加载 elem ...

  2. SPOJ SUMPRO(数学)

    题意: 给出一个数N,问所有满足n/x=y(此处为整除)的所有x*y的总和是多少.对答案mod(1e9+7). 1 <= T <= 500. 1 <= N <= 1e9. 分析 ...

  3. Linux下tomcat的catalina.out屏蔽

    修改catalina.sh ,找到下面的位置: if [ -z "$CATALINA_OUT" ] ; then#CATALINA_OUT="$CATALINA_BASE ...

  4. 对一个deb包的解压、改动、又一次打包全过程方法

    /*********************************************************************  * Author  : Samson  * Date   ...

  5. IntelliJ IDEA在行尾增加分号

    IntelliJ IDEA在行尾增加分号 Ctrl+Shift+Enter - 本身的含义是自动完成,如果需要的话,会在行尾添加分号:

  6. win7如何更改语言教程

    一.首先从桌面左下角的开始菜单中找到“控制面板”,然后打开,如下图所示: 打开电脑控制面板 二.进入控制面板之后,我们再进入“时钟.语言和区域”设置,如下图所示: 电脑语言改成英文方法 三.进入电脑语 ...

  7. JAVA获取操作系统的信息

    列出全部信息: Properties prop = System.getProperties(); prop.list(System.out); 获取某个信息: String os = prop.ge ...

  8. Spring MVC不要在@Service bean中保存状态

    先看这么一段代码: @Service public class AccountService { private String message; public void foo1() { if (tr ...

  9. java的输入输出流(一)

    java中i/o流是java中核心的一部分,曾经学过.可是理解不够深入,渐渐的也就忘了,如今在从新学习下java的io处理,写下我学习的笔记.便于记忆,和总结归纳: 本文原创,转载请注明:http:/ ...

  10. ibatis 入门

     iBatis 简单介绍: iBatis 是apache 的一个开源项目.一个O/R Mapping 解决方式,iBatis 最大的特点就是小巧.上手非常快.假设不须要太多复杂的功能.iBatis ...