[LeetCode] 287. Find the Duplicate Number(Floyd判圈算法)
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:
- You must not modify the array (assume the array is read only).
- You must use only constant, O(1) extra space.
- Your runtime complexity should be less than O(n2
)
. - 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判圈算法)的更多相关文章
- LeetCode 287. Find the Duplicate Number (python 判断环,时间复杂度O(n))
LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...
- Floyd判圈算法
Floyd判圈算法 leetcode 上 编号为202 的happy number 问题,有点意思.happy number 的定义为: A happy number is a number defi ...
- Floyd判圈算法 Floyd Cycle Detection Algorithm
2018-01-13 20:55:56 Floyd判圈算法(Floyd Cycle Detection Algorithm),又称龟兔赛跑算法(Tortoise and Hare Algorithm) ...
- UVA 11549 CALCULATOR CONUNDRUM(Floyd判圈算法)
CALCULATOR CONUNDRUM Alice got a hold of an old calculator that can display n digits. She was bore ...
- leetcode202(Floyd判圈算法(龟兔赛跑算法))
Write an algorithm to determine if a number is "happy". 写出一个算法确定一个数是不是快乐数. A happy number ...
- Floyd 判圈算法
Floyd 判圈算法 摘自维基百科, LeetCode 上 141题 Linked List Cycle 用到这个, 觉得很有意思. 记录一下. 链接: https://zh.wikipedia.or ...
- 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 ...
- UVA 11549 Calculator Conundrum (Floyd判圈算法)
题意:有个老式计算器,每次只能记住一个数字的前n位.现在输入一个整数k,然后反复平方,一直做下去,能得到的最大数是多少.例如,n=1,k=6,那么一次显示:6,3,9,1... 思路:这个题一定会出现 ...
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
随机推荐
- [ZJOI2009]取石子游戏
瞪了题解两三天,直接下转第二篇题解就康懂了 首先我们令 : \(L[i][j]\) 表示当前 \([i,j]\) 区间左侧放置 \(L[i,j]\) 数量的石子后先手必败 \(R[i][j]\) 表示 ...
- MTCNN 人脸检测
demo.py import cv2 from detection.mtcnn import MTCNN # 检测图片中的人脸 def test_image(imgpath): mtcnn = MTC ...
- JS window对象 返回前一个浏览的页面 back()方法
JS window对象 返回前一个浏览的页面 back()方法,加载 history 列表中的前一个 URL. 语法: window.history.back(); 返回前一个浏览的页面 back ...
- AD转换为KiCAD的方法
一.Altium文件转KiCad文件 本文主要介绍: 1.AD文件(SCH和PCB)转换为KiCAD的方法 2.AD封装库转换为KiCAD库的方法 下面让我们进入正题 1.1 PCB的第一种转换方式 ...
- 22pygame 安装
实战步骤 pygame 快速体验 飞机大战 实战 确认模块 --pygame pygame 就是一个 Python 模块, 专为电子游戏设计 提示 : 学习第三方模块, 通常最好的参考资料就在官方网站 ...
- JAVA 关于File的使用
File中常用方法 创建 createNewFile() 在指定位置创建一个空文件,成功就返回true,如果已存在就不创建然后返回false mkdir() 在指定位置创建目录,这只会创建最后一级目录 ...
- redis心得体会
redis简介: 在我们日常的Java Web开发中,无不都是使用数据库来进行数据的存储,由于一般的系统任务中通常不会存在高并发的情况,所以这样看起来并没有什么问题,可是一旦涉及大数据量的需求,比如一 ...
- 载]mysqlhotcopy 热备工具体验与总结
载]mysqlhotcopy 热备工具体验与总结 今天有空尝试了一下MYSQLHOTCOPY这个快速热备MYISAM引擎的工具.(本文是针对单个服务器的情况,以后将会加入多服务器相关操作)他和MYSQ ...
- mysql order by 自定义
TIMESTAMPDIFF 语法: TIMESTAMPDIFF(interval,datetime_expr1,datetime_expr2). 说明: 返回日期或日期时间表达式datetime_ex ...
- Python---字符串拼接和严格字符串
BIF内建函数 Python3中提供了多少个内置函数 68 Python3中TUling tuling是不是一样的 严格区别大小 “=”和“==”的运用与区别 - ‘=‘ 是用来赋值的 - ...