题目

https://leetcode.com/problems/find-the-duplicate-number/

题解

题目有限制 不能修改数组元素,必须 O(1) 空间复杂度,所以 不能排序,不能用 set / map

因为本题具有 所有数字都在 1n 之间 的特点,所以天然的(出题人故意的)可以当做一个链表来处理。

官方题解 利用了与 “找到环形链表的第一个起点” 相同的思路。

Approach 3: Floyd’s Tortoise and Hare (Cycle Detection)

Intuition

The idea is to reduce the problem to Linked List Cycle II:

Given a linked list, return the node where the cycle begins.

First of all, where does the cycle come from? Let’s use the function f(x) = nums[x] to construct the sequence: x, nums[x], nums[nums[x]], nums[nums[nums[x]]], ....

Each new element in the sequence is an element in nums at the index of the previous element.

If one starts from x = nums[0], such a sequence will produce a linked list with a cycle.

The cycle appears because nums contains duplicates. The duplicate node is a cycle entrance.

Here is how it works:

Now the problem is to find the entrance of the cycle.

Algorithm

Floyd’s algorithm consists of two phases and uses two pointers, usually called tortoise and hare.

In phase 1, hare = nums[nums[hare]] is twice as fast as tortoise = nums[tortoise]. Since the hare goes fast, it would be the first one who enters the cycle and starts to run around the cycle. At some point, the tortoise enters the cycle as well, and since it’s moving slower the hare catches the tortoise up at some intersection point. Now phase 1 is over, and the tortoise has lost.

Note that the intersection point is not the cycle entrance in the general case.


To compute the intersection point, let’s note that the hare has traversed twice as many nodes as the tortoise, i.e. 2d(tortoise)=d(hare), that means

(F+a)=F+nC+a, where n is some integer.

Hence the coordinate of the intersection point is F+a=nC.

In phase 2, we give the tortoise a second chance by slowing down the hare, so that it now moves with the speed of tortoise: tortoise = nums[tortoise], hare = nums[hare]. The tortoise is back at the starting position, and the hare starts from the intersection point.

Let’s show that this time they meet at the cycle entrance after F steps.

  • The tortoise started from zero, so its position after F steps is F.
  • The hare started at the intersection point F+a=nC, so its position after F steps is nC+F, that is the same point as F.
  • So the tortoise and the (slowed down) hare will meet at the entrance of the cycle.

Implementation

class Solution {public int findDuplicate(int[] nums) {int fast = nums[0];int slow = nums[0];do {fast = nums[nums[fast]];slow = nums[slow];} while (fast != slow);slow = nums[0];while (fast != slow) {fast = nums[fast];slow = nums[slow];}return fast;}
}

Result

leetcode 287. Find the Duplicate Number | 287. 寻找重复数(判断链表是否有环,并找到环的起点)相关推荐

  1. LeetCode 287. Find the Duplicate Number (时间复杂度O(n)) + 链表判断环

    LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...

  2. 287. **Find the Duplicate Number

    287. **Find the Duplicate Number https://leetcode.com/problems/find-the-duplicate-number/description ...

  3. LeetCode 287. Find the Duplicate Number

    题目: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), ...

  4. LeetCode.287 Find the Duplicate Number

    题目: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), ...

  5. 287. Find the Duplicate Number

    Title 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数.假设只有一个重复的整数,找出这个重复的数. 示例 1: 输入 ...

  6. LeetCode 287---Find the Duplicate Number

    问题链接:LeetCode 287-Find the Duplicate Number 题目大意 : 找出序列中唯一一个重复出现的数字,且只能使用o(1)的额外空间 实现代码如下: public cl ...

  7. [LeetCode]快慢指针笔记[142和287]

    这里是LeeTioN的博客 快慢指针一般用于判断一个链表是否存在环的问题 142. Linked List Cycle II 思路 在寻找环的入口时,我们将设置两个指针,快指针和慢指针.初始化时,将快 ...

  8. 【LeetCode】【HOT】287. 寻找重复数(抽象环形链表)

    [LeetCode][HOT]287. 寻找重复数 文章目录 [LeetCode][HOT]287. 寻找重复数 package hot;public class Solution287 {publi ...

  9. 【leetcode】287. 寻找重复数

    题目链接:传送门 题目描述: 给定一个数组 nums 包含 n + 1 个整数,每个整数在 1 到 n 之间,包括 1 和 n.现在假设数组中存在一个重复的数字,找到该重复的数字. 注意 不能修改数组 ...

最新文章

  1. 跨域部署Silverlight时需要注意的问题
  2. Ubuntu安装TensorFlow-gpu及cuda9.0(不给自己找麻烦)
  3. 前沿科技 | 中科院科学家研究揭示灵活行动选择的神经机制
  4. Thinking In Machine Learning
  5. 北邮高级语言设计基于java期末_北邮《高级语言程序设计》第三次阶段作业带答案...
  6. exsi vmfs 没了_它是“推粪机”,隔三差五吃一盘,口臭没了排出宿便,肠道变干净...
  7. sklearn应用—高斯混合
  8. ARC107——C - Shuffle Permutation
  9. 当try和finally都包含return时
  10. python图像转字符画_Python实现图片转字符画的示例
  11. RTP 包格式 详细解析
  12. 图像处理13:非极大值抑制
  13. opencv学习——翻转摄像头
  14. TOEFL wordlist 35
  15. 【图解】共模干扰,差模干扰
  16. Python处理txt文件:多句分行+行首尾添加字符+for循环--Python新手自练系列
  17. 未来十年计算机体系结构的历史和趋势
  18. 幻影成像的搭建以及应用
  19. 大咖说*图书分享-Node布道师狼叔|三卷书详解Node.js
  20. 去除/屏蔽 安卓/Android手机的 qq浏览器 微信H5使用video标签播放视频,结束后的视频广告

热门文章

  1. HDU - 6186 CS Course(维护前缀+后缀)
  2. mysql innodb索引覆盖_Mysql InnoDB 覆盖索引与回表
  3. PTA-习题11-2 查找星期 (15 分)-enum
  4. Delphi 按Esc快捷键退出程序的简单方法
  5. C++虚继承(五) --- 虚拟继承的概念
  6. Netty学习笔记(六)Pipeline的传播机制
  7. 经典|Linux:为什么性能工具需要 BPF 技术(送多本)
  8. 一个内核网络漏洞详解|容器逃逸
  9. 美团/力扣(647)--回文字串
  10. 实现 LRU 缓存机制