传送门

Description

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(n2).
  4. There is only one duplicate number in the array, but it could be repeated more than once.

思路

题意:给定一个数组,包含n + 1个数,其数值在1-n之间,证明至少存在一个重复的数。假设仅有一个重复的数,找出它。

要求:

  • 假设数组仅为可读,不允许改变数组
  • 空间复杂度为O(1),时间复杂度要求小于O(n2)

题解:由于不允许改变数组,因此不能将数组排序,又因为额外的空间仅允许O(1),因此,不考虑hash。复杂度不能为O(n2),所以不能暴力求解。

方法一:为了降低复杂度,我们可以考虑二分,将复杂度降低为O(nlogn),每次二分,然后遍历数组,查看小于等于mid的数,如果个数小于等于mid,则证明重复的数小于等于mid,反之在[mid + 1,right]的区间。

方法二:此种方法利用floyd判圈算法的原理来求解,具体可以查看这里:click here

class Solution {
public:
//9ms
int findDuplicate(vector<int>& nums) {
if (nums.size() > ){
int slow = nums[],fast = nums[nums[]];
while (slow != fast){
slow = nums[slow];
fast = nums[nums[fast]];
}
fast = ;
while (slow != fast){
slow = nums[slow];
fast = nums[fast];
}
return slow;
}
return -;
} //9ms
int findDuplicate(vector<int>& nums) {
int left = ,right = nums.size() - ;
while (left < right - ){
int mid = left + ((right - left) >> );
int cnt = ;
for (auto val : nums){
if (val <= mid) cnt++;
}
if (cnt <= mid) left = mid;
else right = mid;
}
return left;
}
};

[LeetCode] 287. Find the Duplicate Number(Floyd判圈算法)的更多相关文章

  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. Floyd判圈算法

    Floyd判圈算法 leetcode 上 编号为202 的happy number 问题,有点意思.happy number 的定义为: A happy number is a number defi ...

  3. Floyd判圈算法 Floyd Cycle Detection Algorithm

    2018-01-13 20:55:56 Floyd判圈算法(Floyd Cycle Detection Algorithm),又称龟兔赛跑算法(Tortoise and Hare Algorithm) ...

  4. UVA 11549 CALCULATOR CONUNDRUM(Floyd判圈算法)

    CALCULATOR CONUNDRUM   Alice got a hold of an old calculator that can display n digits. She was bore ...

  5. leetcode202(Floyd判圈算法(龟兔赛跑算法))

    Write an algorithm to determine if a number is "happy". 写出一个算法确定一个数是不是快乐数. A happy number ...

  6. Floyd 判圈算法

    Floyd 判圈算法 摘自维基百科, LeetCode 上 141题 Linked List Cycle 用到这个, 觉得很有意思. 记录一下. 链接: https://zh.wikipedia.or ...

  7. SGU 455 Sequence analysis(Cycle detection,floyd判圈算法)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=455 Due to the slow 'mod' and 'div' operati ...

  8. UVA 11549 Calculator Conundrum (Floyd判圈算法)

    题意:有个老式计算器,每次只能记住一个数字的前n位.现在输入一个整数k,然后反复平方,一直做下去,能得到的最大数是多少.例如,n=1,k=6,那么一次显示:6,3,9,1... 思路:这个题一定会出现 ...

  9. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

随机推荐

  1. Codeforces 429E(欧拉回路)

    题面 传送门 题目大意: 有n条线段,每条线段染红色或蓝色,使得数轴上每个点被红色线段覆盖的次数与被蓝色线段覆盖数差的绝对值小于等于1.输出染色方案. 分析 题意其实可以这样理解: 一段初始全为0 的 ...

  2. linux学习笔记(1):

    一.Linux系统简介 1.什么是linux Linux是一个免费的.多用户.多任务的操作系统,其运行方式.功能和UNIX系统很相似,但Linux系统的稳定性.安全性与网络功能是许多商业操作系统所无法 ...

  3. .net 项目中应用Web Services(vs2012)

    一.在asp.net项目中添加Web services1.新建一个asp.net项目(目前尚未验证是否可以在MVC项目中添加)2.在项目名上右击,选择添加→新建项→Web服务,输好名称后确定即可 二. ...

  4. 21eval 函数

    eval() 函数十分强大 ---- 将字符串 当成 有效的表达式 来求职 并 返回计算结果 # 基本的数学计算 # 字符串重复 print(eval("'*' * 5")) # ...

  5. css 块级格式化上下文(BFC)

    一.块级格式化上下文(BFC) 1.什么是块级格式化上下文? Block Formatting Contexts (BFC,块级格式化上下文)就是一个块级元素 的渲染显示规则 (可以把 BFC 理解为 ...

  6. SpringBoot---异步消息

    1.概述 1.1.SpringBoot  对 JMS 的自动配置  位于  org.springframework.boot.autoconfigure.jms下: 1.2.SpringBoot  支 ...

  7. JMeter-性能测试之报表设定的注意事项

    在使用 Jmeter 执行性能测试时,需要屏蔽以下模块: 结果树 图形结果 断言 具体的说明,可以见官网:http://jmeter.apache.org/usermanual/component_r ...

  8. Oracle12c创建及删除PDB

    在12C R1版本中只支持Global Shared Undo模式,所有container共享一个UNDO表空间:在12C R2引入了PDB Local Undo模式,每个container都有自己的 ...

  9. 路由Vue-router 的使用总结

    1.关于 router-view 匹配 vue 项目使用 vue-router,所有的根级别的路由都是在 App.vue 文件中的 router-view 中渲染的.比如下面的 path: '/' . ...

  10. 【LOMBOK】能引入 @Slf4j 注解,不能识别 log 的解决方法

    问题: 在pom.xml中加入引入了lombok的依赖,可以引用@Slf4j注解不能识别log 如:注:上面一篇博客,已经说明lombok的安装了,但是用的时候还有点问题. 1).把lombok.ja ...