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.

==============================

题目:如果把这个数组看做是链表,数组中元素值当做链表节点中next指针,

我们可以发现这道题就是在求 链表中环的  开始节点

有关环的开始节点证明,可以看这里:  [http://www.cnblogs.com/li-daphne/p/5551048.html]

=========

思路:利用fast/slow方法(slow的步长为1,fast的步长为2),找到"链表"相遇的地方,

这时候fast指向"链表"的开始位置,slow接着遍历(fast和slow的步长都为一).

当fast/slow再次相遇的地方,就是"链表环"的入口,即数组中 重复元素.

===========

代码:

  1. class Solution {
  2. public:
  3. int findDuplicate(vector<int>& nums) {
  4. if(nums.size()>){
  5. int fast = nums[nums[]];
  6. int slow = nums[];
  7. while(slow!=fast){
  8. slow = nums[slow];
  9. fast = nums[nums[fast]];
  10. }
  11.  
  12. fast = ;
  13. while(fast != slow){
  14. fast = nums[fast];
  15. slow = nums[slow];
  16. }
  17. return slow;
  18. }
  19. return -;
  20. }
  21. };

287. Find the Duplicate Number的更多相关文章

  1. 287. Find the Duplicate Number hard

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

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

  3. LeetCode 287. Find the Duplicate Number (python 判断环,时间复杂度O(n))

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

  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 解题思路

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

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

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

  7. 287. Find the Duplicate Number 找出数组中的重复数字

    [抄题]: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive ...

  8. 【LeetCode】287. Find the Duplicate Number

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

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

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

随机推荐

  1. ZOJ 1029 Moving Tables

    原题链接 题目大意:走廊两边排列了400个房间,要在两个房间之间搬桌子.搬桌子的时候会占用一部分走廊,有冲突的话要回避.求最快搬完的时间. 解法:开辟一个数组,每占用一段走廊,就把相应的房间号的元素加 ...

  2. ZOJ 1005 Jugs

    原题链接 题目大意:有一大一小两个杯子,相互倒水,直到其中一个杯子里剩下特定体积的水.描述这个过程. 解法:因为两个杯子的容积互质,所以只要用小杯子不断往大杯子倒水,大杯子灌满后就清空,大杯子里迟早会 ...

  3. C++ Primer : 第十二章 : 动态内存之shared_ptr与new的结合使用、智能指针异常

    shared_ptr和new结合使用 一个shared_ptr默认初始化为一个空指针.我们也可以使用new返回的指针来初始化一个shared_ptr: shared_ptr<double> ...

  4. python爬虫抓网页的总结

    python爬虫抓网页的总结 更多 python 爬虫   学用python也有3个多月了,用得最多的还是各类爬虫脚本:写过抓代理本机验证的脚本,写过在discuz论坛中自动登录自动发贴的脚本,写过自 ...

  5. POJ 1043 What's In A Name?(唯一的最大匹配方法)

                                                            What's In A Name? Time Limit: 1000MS   Memor ...

  6. linux环境进程的停止

    使用 #ps auxf|grep 你想要获取的进程,如下,我想要获得的是nginx的进程号 图中黄色的便是进程号, 在此我们想kill掉主进程就要把后面有master字样的进程号kill掉 命令如下 ...

  7. 【LOI2005】【P1306】河流

    树归题,本来比较简单,但是因为几个思想搞错了,所以卡了两天 原题: 几乎整个Byteland 王国都被森林和河流所覆盖.小点的河汇聚到一起,形成了稍大点的河.就这样,所有的河水都汇聚并流进了一条大河, ...

  8. 100 open source Big Data architecture papers for data professionals

    zhuan :https://www.linkedin.com/pulse/100-open-source-big-data-architecture-papers-anil-madan Big Da ...

  9. MySQL数据库InnoDB存储引擎多版本控制(MVCC)实现原理分析

    文/何登成 导读:   来自网易研究院的MySQL内核技术研究人何登成,把MySQL数据库InnoDB存储引擎的多版本控制(简称:MVCC)实现原理,做了深入的研究与详细的文字图表分析,方便大家理解I ...

  10. Web前端开发笔试&面试_04

    >>XDL: 1.在CSS中,选择器的优先级?(如important,id,class 这些……) 2.如何消除行内间隙? Inline-block 3. 如何清除浮动? —— 4. CS ...