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. 2015GitWebRTC编译实录7

    2015.07.20 libvoiceengine 编译通过去除了mock测试代码,mock是用来进行测试的,意义不大.另外会报一个常量错误,需要定义WEBRTC_MAC宏,只定义WEBRTC_IOS ...

  2. zookeeper的 目录加密

    import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.WatchedEvent;import org.apache.zo ...

  3. MySQL数据库高并发优化配置

    在Apache, PHP, mysql的体系架构中,MySQL对于性能的影响最大,也是关键的核心部分.对于Discuz!论坛程序也是如此,MySQL的设置是否合理优化,直接 影响到论坛的速度和承载量! ...

  4. (转)Tarjan应用:求割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)

    基本概念: 1.割点:若删掉某点后,原连通图分裂为多个子图,则称该点为割点. 2.割点集合:在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成多个 ...

  5. js的数组操作 splice

    原文:点击打开链接 1.作用:从指定位置删除部分元素并增加新的元素                1.1.该方法返回值是被删除的元素组成的数组                1.2.splice是直接 ...

  6. 【转】NSString属性什么时候用copy,什么时候用strong?

    原文网址:http://www.cocoachina.com/ios/20150512/11805.html 我们在声明一个NSString属性时,对于其内存相关特性,通常有两种选择(基于ARC环境) ...

  7. Spark 优化器 ML的论文

    http://people.csail.mit.edu/matei/papers/2015/sigmod_spark_sql.pdf http://www.vldb.org/pvldb/vol4/p5 ...

  8. SSE入门

    此文主要内容来自这篇文章,本文翻译只求能理解,不求逐句翻译. 正文: 我们将在本文中介绍如何在C++/C中使用SSE指令.我的目的不是用SSE写尽可能快的程序,而是试图讲明白它的使用方法. 什么是SS ...

  9. C#→关于System.Data.Linq下的Table<TEntity> 泛型类 的问题

    Table<TEntity>表示表格记录,它是一个泛型集合类,它的元素就是表格实体对象.它提供一组方法,对元素进行添加删除操作,并可以通过DataContext将这些操作保存到数据库. 表 ...

  10. JS之变量的运算

    js变量的特点: 1.区分大小写,这是与html及css最大的不同: 2.弱变量.通过var进行定义,无明确的数据类型. 第一部分 字符型 对于字符型的数据,常用的操作为字符的转换.字符的操作 1.字 ...