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再次相遇的地方,就是"链表环"的入口,即数组中 重复元素.

===========

代码:

class Solution {
public:
int findDuplicate(vector<int>& nums) {
if(nums.size()>){
int fast = nums[nums[]];
int slow = nums[];
while(slow!=fast){
slow = nums[slow];
fast = nums[nums[fast]];
} fast = ;
while(fast != slow){
fast = nums[fast];
slow = nums[slow];
}
return slow;
}
return -;
}
};

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. Android EditText内容监听

    监听 EditText的内容变化,作出对应的处理. MainActivity.class package com.example.edittextdemo; import android.app.Ac ...

  2. sqlserver函数

    SQLServer时间日期函数详解,SQLServer,时间日期, 1.      当前系统日期.时间 select getdate() 2. dateadd      在向指定日期加上一段时间的基础 ...

  3. Python科学画图小结

    Python画图主要用到matplotlib这个库.具体来说是pylab和pyplot这两个子库.这两个库可以满足基本的画图需求,而条形图,散点图等特殊图,下面再单独具体介绍. 首先给出pylab神器 ...

  4. spark中streamingContext的使用详解

    两种创建方式 val conf = new SparkConf().setAppName(appName).setMaster(master);val ssc = new StreamingConte ...

  5. C++@命名空间(转)

    转自http://hi.baidu.com/rainysky_2006/blog/item/a490e01fc3de7964f724e4d1.html 本讲基本要求 * 掌握:命名空间的作用及定义:如 ...

  6. Python实现__metaclass__实现方法运行时间统计

    几天前写的,参考了园友的一篇文章,链接找不到了.先感谢,找到了链接再补上.

  7. 黑马程序员——JAVA基础之Collections和Arrays,数组集合的转换

    ------- android培训.java培训.期待与您交流! ---------- 集合框架的工具类:        Collections : 集合框架的工具类.里面定义的都是静态方法. Col ...

  8. InputStreamReader/OutputStreamWriter乱码问题解决

    /* *InputStreamReader参数charset要跟文件编码格式一致. InputStreamReader读的时候才不会乱码. *OutputStreamWriter参数charset设置 ...

  9. loadrunner---<二>---菜鸟对cookie的思考

    http://www.cnblogs.com/Pierre-de-Ronsard/archive/2012/11/19/2772630.html loadrunner---<二>---菜鸟 ...

  10. C#学习之初步理解委托、事件、匿名方法和Lambda

    最经在学习LinqtoSql,然后扯到Lambda表达式,然后扯到匿名方法,然后扯到委托,最后扯到事件处理...后来发现对委托这个概念和事件处理这个过程理解得不是很清晰,遂得一下学习笔记.那里说得不对 ...