[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判圈 ...
随机推荐
- Codeforces 984D 题解(DP)
题面 传送门 题目大意: 给你一个计算区间f函数的公式,举例f(1,2,4,8)=f(1⊕2,2⊕4,4⊕8)=f(3,6,12)=f(3⊕6,6⊕12)=f(5,10)=f(5⊕10)=f(15)= ...
- IOC详解
Ioc--控制反转详解(转载 http://www.cnblogs.com/qinqinmeiren/archive/2011/04/02/2151697.html) 本文转载与百度知道,简单例子让 ...
- Java解析XML介绍
开发十年,就只剩下这套架构体系了! >>> XML解析器提供了访问或修改用来表示数据的xml文件的能力.Java中提供了多种方式来解析xml文件. 主要分为两类,包括解析XML文 ...
- vue自定义指令获取焦点及过滤器修改时间
<template id="comp3"> <div id="app"> <model :list="selectedl ...
- VUE CLI3.X 创建项目
Node.js环境搭建 Node.js基于V8引擎,可以让js代码脱离浏览器运行 Vue CLI3.0 需要Node.js 8.9或者更高版本. 用nvm或者nvm-windows在同一台电脑中管理多 ...
- linux针对TCP和 UDP的调优
- open, creat - 用来 打开和创建 一个 文件或设备
SYNOPSIS 总览 #includ e <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int o ...
- Java虚拟机——类加载机制
转自:http://blog.csdn.net/ns_code/article/details/17881581 类加载过程 类从被加载到虚拟机内存中开始,到卸载出内存为止,它的整个生命周期包括:加载 ...
- tailf 跟踪日志文件
1.命令功能 tailf 跟踪日志文件增长,作用跟tail –f相同.tailf将输出文件的最后10行,然后等待文件增长. 2.语法格式 tailf option file 参数说明 参数 参数说 ...
- Task4.文本表示:从one-hot到word2vec
参考:https://blog.csdn.net/wxyangid/article/details/80209156 1.one-hot编码 中文名叫独热编码.一位有效编码.方法是使用N位状态寄存器来 ...