LeetCode 287. Find the Duplicate Number

暴力解法

时间 O(nlog(n)),空间O(n),按题目中Note“只用O(1)的空间”,照理是过不了的,但是可能判题并没有卡空间复杂度,所以也能AC。

class Solution:
# 基本思路为,将第一次出现的数字
def findDuplicate(self, nums: List[int]) -> int:
s = set()
for i in nums:
a = i in s
if a == True:
return i
else:
s.add(i)

双指针判断环

时间O(n),空间O(1),思路十分巧妙,但是使用条件比较苛刻。根据题目给出的条件,恰好能用这种解法,这应该也是出题人推荐的解法。

题意分析:

  1. 输入的序列有n+1个数字,每个数字在1~n之间取,这为构成数字环创造了条件。
  2. 只有一个数字有重复,所以只可能构成一个环。

注:上面所说的环是指1->2->3->1

以样例1为例:[1,3,4,2,2]

0 1 2 3 4
1 3 4 2 2

如下图所示,其中2->4->2构成环,入环点为2

解题思路

由题意分析可知,每个样例都可以画成这样一张图,我们只需要找出图中的环,并找出入环点,即为所求的重复数字key,下面都用key表示所求的重复数字。

为什么必定存在环

以样例1为例,图中出现了5个点0-4,图中存在5根指针线,5个点5根线,必定存在环。

n个点,点的范围去0~n-1,n根线,必定存在环。(n-1根线是恰好无环的情况,自己画图可知)

找环的方法

设置一个慢指针slow,一个快指针fast。slow每次走一步,fast每次走两步,如果slow与fast能相遇,说明图中存在环,并且相遇点一定存在于环中。

为什么key一定为入环点?

有题意分析中的表可知,key的入度一定大于1,即不止一个点可以直接到key。而key一定存在于环中,所以key一定为入环点。样例1中3,4都可到达2,2的入度2,2为入环点,即为所求的key。

怎么找入环点key?

slow和fast相交的点记为相遇点P。

slow和fast从起点0到相遇点P运行步骤如下:

这个相遇点P与起点0到达入环点key的步数 差距为环L的整数倍,故设置slow2从起点0开始,每次走一步,slow从相遇点P开始,每次走一步,slow和slow2一定会相遇在入环点key。

我们可以有一个小小的证明,如下图

设起点0到达入环点key的步数为x,相遇点P到达入环点key的步数为y。

设slow指针走到相遇点P的步数为t,fast走到相遇点P的步数为2*t。

设走完环一圈的步数为L

2 * t - x + y = M * L(一)

t - x + y = N * L (二)

fast指针在环中走的步数2t-x,此时到达相遇点P,key->P->key步数为2t-x+y = M * L,正好为L的M倍,M为常数。(一)式

slow指针在环中走的步数t-x,此时到达相遇点P,key->P->key步数为t-x+y = N * L,正好为L的N倍,N为常数。(二)式

2倍(二)式 减 (一)式

y-x = (2N-M) * L

所以y与x的步数差距为L倍的环。

得证。

如何确定起点0一定会进入包含key的环?

假设存在不包含key的环,起点0在不包含key的环中绕圈。

0 a1 a2 a3 a4 a5 a6
b1 b2 b3 b4 b5 b6 b7

按题意不包含环,b[i]与b[j]一定不相等(i != j)

由于b1~b7从1开始,所以b[i]只能从a[j]中取(1<=i<=7,1<=j<=6)

从6个数字的集合a中取7个数字,所以假设不成立,必定存在相同数字b[k],即为key。

代码如下

class Solution:
def findDuplicate(self, nums: List[int]) -> int:
# 如果只有两个元素,第一个元素一定是重复元素
if len(nums) == 2:
return nums[0] # fast每次走两步,slow每次走一步,起始点可以为任意位置
fast = 0
slow = 0
# python没有do while,所以在循环外写了一遍
slow = nums[slow]
fast = nums[nums[fast]]
while slow != fast:
slow = nums[slow]
fast = nums[nums[fast]] # fast从起点每次走一步,一定会与slow相遇,此时slow可能在环中走了多倍的L步。
# L为环一圈的步数
fast = 0
while fast != slow:
slow = nums[slow]
fast = nums[fast]
return fast

LeetCode 287. Find the Duplicate Number (python 判断环,时间复杂度O(n))的更多相关文章

  1. [LeetCode] 287. Find the Duplicate Number(Floyd判圈算法)

    传送门 Description Given an array nums containing n + 1 integers where each integer is between 1 and n  ...

  2. [LeetCode] 287. Find the Duplicate Number 寻找重复数

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

  3. LeetCode 287. Find the Duplicate Number (找到重复的数字)

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

  4. [LeetCode] 287. Find the Duplicate Number 解题思路

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

  5. LeetCode : 287. Find the Duplicate Number

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAACRAAAAMMCAYAAAAhQhmZAAAMFGlDQ1BJQ0MgUHJvZmlsZQAASImVlw ...

  6. 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 ...

  7. 287. Find the Duplicate Number hard

    287. Find the Duplicate Number   hard http://www.cnblogs.com/grandyang/p/4843654.html 51. N-Queens h ...

  8. 【LeetCode】287. Find the Duplicate Number 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存已经访问过的数字 链表成环 二分查找 日期 题目 ...

  9. 【LeetCode】287. Find the Duplicate Number

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Given an array nums containing n + 1 integer ...

随机推荐

  1. Java 8 函数接口详细教程

    ay = new byte[array.length]; for (int i = 0; i < array.length; i++) { transformedArray[i] = funct ...

  2. Linux下文件查找命令find笔记

    在Linux命令下如果需要快速自己系统所需要处理的文件,可以通过find命令快速进行检索. 如果想在某个路径下查找相应的文件可以执行如下命令: find path -name filename # p ...

  3. Html 页面刷新后出现闪动

    Html 页面刷新后,或跳转后,出现闪动,抖动问题 1.查看有没有用到新字体,新字体链接位置是否存在 如: @font-face { font-family: "AvantGarde-Dem ...

  4. PAT_A1144#The Missing Number

    Source: PAT A1144 The Missing Number (20 分) Description: Given N integers, you are supposed to find ...

  5. 【剑指Offer】45、扑克牌顺子

      题目描述:   LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到 ...

  6. 操作符重载(day07)

    二十 操作符重载 eg:复数x+yi +4i (+2i) + (+4i) = +6i 双目操作符(L # R) 1.1 运算类的双目操作符:+ - * / -->左右操作数可以是左值也可以是右值 ...

  7. HDU2866 Special Prime

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=2866 题意:在区间[2,L]内,有多少个素数p,满足方程有解. 分析: 原方程变为: n^(b-1) ...

  8. BeanUtils.copyProperties()错误使用,给自己挖了坑

    场景:需要对某个集合中的所有元素拷贝到另一个集合中,想着BeanUtils.copyProperties()可以深拷贝对象,误以为也可以拷贝集合,于是乎写下了如下代码 List<CostRule ...

  9. UOJ #219 BZOJ 4650 luogu P1117 [NOI2016]优秀的拆分 (后缀数组、ST表)

    连NOI Day1T1都不会做...看了题解都写不出来还要抄Claris的代码.. 题目链接: (luogu)https://www.luogu.org/problemnew/show/P1117 ( ...

  10. luogu P4726 多项式指数函数(模板题FFT、多项式求逆、多项式对数函数)

    手动博客搬家: 本文发表于20181127 08:39:42, 原地址https://blog.csdn.net/suncongbo/article/details/84559818 题目链接: ht ...