一个长度为 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. 微信浏览器video

    <style> /* 解决上下有黑边,不能全屏 */ video{object-fit: fill;} </style> <video id="videoID& ...

  2. Java 实现一个链表

    public class MyList { static class Node {// 节点类 Object data; Node next; public Node(Object data) {// ...

  3. 六:二叉树中第k层节点个数与二叉树叶子节点个数

    二叉树中第k层节点个数 递归解法: (1)假设二叉树为空或者k<1返回0 (2)假设二叉树不为空而且k==1.返回1 (3)假设二叉树不为空且k>1,返回左子树中k-1层的节点个数与右子树 ...

  4. 浅析分布式数据库中间件DDM

    前言 DDM是什么?这是华为云Paas推出的分布式数据库中间件,DDM(Distributed Database Middleware)是一个实现了Mysql协议栈的服务器,前端用户可以把它看做一个数 ...

  5. Codeforces Round #319 (Div. 2)B. Modulo Sum DP

                                                             B. Modulo Sum                               ...

  6. 【OI】线性筛

    如何查找一个范围内的所有素数? 可以是从1~n挨个判断n%i 是否 == 0,也可以从 1~sqr(n) 一个个判断. 相信你们也听说过埃氏筛法,是使用每一个数的倍数筛掉合数!但是!每一个合数要被筛多 ...

  7. MessageBox.Show

    MessageBox.Show()共有21中重载方法.现将其常见用法总结如下: .MessageBox.Show("Hello~~~~"); 最简单的,只显示提示信息. .Mess ...

  8. 利用JFreeChart生成多轴图表(7) (转自 JSP开发技术大全)

    利用JFreeChart生成多轴图表(7) (转自 JSP开发技术大全) 14.7 利用JFreeChart生成多轴图表 通过JFreeChart插件还可以生成拥有多个坐标轴的图表,简称多轴图表.在生 ...

  9. 4. extjs中form中的frame:true表示什么

    转自:https://blog.csdn.net/qiu512300471/article/details/23737217 设置为true时可以为panel添加背景色.圆角边框等,如下图 下面的是f ...

  10. 4-2 买家类目-dao(下)

    查询出来的对象ProductCategory就已经有updateTime和createTime了,然而你只是把对象的categoryType给修改了一下,修改之后就执行save方法保存了.所以它还是原 ...