一个数组中的长度是n+1,里面存放的数字大小的范围是【1,n】,根据鸽巢原理,所以里面肯定有重复的数字,现在预定重复的数字就1个,让你找到这个数字!

http://bookshadow.com/weblog/2015/09/28/leetcode-find-duplicate-number/

使用坐标和数值之间的相互转换!计算机界广为人知的环检测的问题。

网上对这个问题的解释太多了,一搜一大把,环检测方法才是真是的解题之道,环解决方法在上面的链接中有,但是让人茅塞顿开的一个点是在这种解法中是如何合理使用了下标0!!!!还是先分析下这个题目:是说有1到n,n个数字,然后数组的size是n+1的,如果玩过那种动态的拼图游戏,那么0这个位置就是拼图盘上那个永远的空位呀!!!假设一种场景是重复的数字只重复1次,那么我们各个数不断各种转化之后,那么在0这个位置的就是多出来的数字呀!那么攻略中提出来的办法就是找到后面环的入口了!要怎么找呢?!下标从1->n中一定会有环呢?这个是要证明的!这是因为题目中已经说了存在i和j,使得a[i]=a[j]呀,那么怎么保证i和j一定是在这个环上的呢?【这也是该想法跑不到我们脑子里的原因】还是因为题目中的要求太紧了,1->n的数字,散落在0,1->n的空间里,从0开始跳,那么很可能是一下子就跳到了重复元素也可能不是,但是任你怎么坚持到最后,环的入口必然来临,在下面这种场景:

0 1 2 3 4

3 4 1 2 4

a[0] = 3; a[3] = 2; a[2] = 1; a[1] = 4; a[4] = 4;

你侥幸地想着怎么让元素不形成环,但是发现逼到最后,你消耗完了所有的1->n,没办法了,最后一个位置只能从1->n中任选一个填上!

那么就用经典的链表的解决方法来解吧。

环检测的问题:典型场景-->链表

算法(9)Find the Duplicate Number的更多相关文章

  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. 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. Find the Duplicate Number 解答

    Question Given an array nums containing n + 1 integers where each integer is between 1 and n (inclus ...

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

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

  6. LeetCode(287)Find the Duplicate Number

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

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

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

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

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

  9. 287. Find the Duplicate Number hard

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

随机推荐

  1. 【2017 ICPC亚洲区域赛沈阳站 K】Rabbits(思维)

    Problem Description Here N (N ≥ 3) rabbits are playing by the river. They are playing on a number li ...

  2. XPath知识点简单总结(思维导图)

    XPath是一种用于在XML文档中查找信息的语言,其对HTML也有很好的支持,所以在网络爬虫中可用于解析HTML文档.参考链接. 下图是XPath知识点的简单总结成思维导图:

  3. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column

    com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column …… 出现这个异常的很大可能性是 数据库是没有问题的 ...

  4. I/O流、ZIP文档

    1) ZIP文档通常以压缩格式存储一个或多个文档.在Java中可以用ZipInputStream读入ZIP文档(即解压文件流),用ZipOutputStream写入ZIP文档(即压缩文件流),无论解压 ...

  5. php-5.6.26源代码 - include_once、require_once、include、require、eval 的opcode处理器

    # ZEND_INCLUDE_OR_EVAL_SPEC_CONST_HANDLER 实现在文件 php-\Zend\zend_vm_execute.h static int ZEND_FASTCALL ...

  6. Hadoop(20)-MapReduce框架原理-OutputFormat

    1.outputFormat接口实现类 2.自定义outputFormat 步骤: 1). 定义一个类继承FileOutputFormat 2). 定义一个类继承RecordWrite,重写write ...

  7. JAVA 泛型之类型擦除

    ★ 泛型是 JDK 1.5 版本引进的概念,之前是没有泛型的概念的,但泛型代码能够很好地和之前版本的代码很好地兼容. CollectionTest.java ---编译成CollectionTest. ...

  8. c#获取当前运行程序所在的目录

    C#获取项目程序及运行路径的方 1.asp.net webform用“Request.PhysicalApplicationPath获取站点所在虚拟目录的物理路径,最后包含“\”:   2.c# wi ...

  9. Lambda表达式详解【转】

    前言 1.天真热,程序员活着不易,星期天,也要顶着火辣辣的太阳,总结这些东西. 2.夸夸lambda吧:简化了匿名委托的使用,让你让代码更加简洁,优雅.据说它是微软自c#1.0后新增的最重要的功能之一 ...

  10. Markdown 基本用法

    声明:引自 http://www.cnblogs.com/hnrainll/p/3514637.html ,感谢!   1. 标题设置(让字体变大,和word的标题意思一样)在Markdown当中设置 ...