算法(9)Find the Duplicate Number
一个数组中的长度是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的更多相关文章
- [LeetCode] 287. Find the Duplicate Number(Floyd判圈算法)
传送门 Description Given an array nums containing n + 1 integers where each integer is between 1 and n ...
- Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- [LeetCode] 287. Find the Duplicate Number 解题思路
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- Find the Duplicate Number 解答
Question Given an array nums containing n + 1 integers where each integer is between 1 and n (inclus ...
- 287. Find the Duplicate Number 找出数组中的重复数字
[抄题]: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive ...
- LeetCode(287)Find the Duplicate Number
题目 Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), ...
- 【LeetCode】287. Find the Duplicate Number 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存已经访问过的数字 链表成环 二分查找 日期 题目 ...
- [LeetCode] Find the Duplicate Number 寻找重复数
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- 287. Find the Duplicate Number hard
287. Find the Duplicate Number hard http://www.cnblogs.com/grandyang/p/4843654.html 51. N-Queens h ...
随机推荐
- BZOJ2580: [Usaco2012 Jan]Video Game(AC自动机)
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 159 Solved: 110[Submit][Status][Discuss] Descriptio ...
- 背景qwq
- MySQL-5.7.20主从复制测试[20180110]
前言 MySQL 5.7.20测试主从复制 环境 主库 192.168.1.59 t-xi-sonar01 从库 192.168.1.51 t-xi-orc01 设 ...
- linux命令之文件系统权限操作常用命令
1. umask:设置权限掩码 语法:umask [参数] 命令说明:umask可以单独使用,可以设置目录与文件的默认权限,默认权限掩码是022,所以默认目录权限是777-022=755,读权限是 ...
- 【PBR的基本配置】
PBR基于策略路由的配置 一:基于报文协议的本地PBR 1:首先进行理论分析:在SW1上利用基于报文报文协议类型的PBR,在sw1与sw3的连接链路上,利用acl制定允许tcp的报文通过3000,并与 ...
- 【c学习-13】
/*库函数 1:数学函数库:math.h abs():绝对值; acos(),asin(),atan():cos,sin,tan的倒数 exp():指数的次幂 pow(x,y):x的y次幂 log() ...
- 【c学习-10】
#include #include #define SOURCE 0 //递归函数 /* [基本类型 [整型(int,[长整型(long int), [短整型(short int),长度整型(long ...
- MySql指令的执行顺序
1:From 2:On 3:Join 4:Where 5:Group by 5.1:函数 6:Having 7:Select 8:Distinct 9:Order by
- 关于Vue的Render的讲解
首先我们传统的对于DOM的操作基本上都是通过js直接的获取一个节点,然后对DOM进行增加或者是删除.而Vue的Render这个函数是通过js虚拟的添加dom节点,然后虚拟的添加到html节点上去. 算 ...
- php导出excel长数字串显示为科学计数方法与最终解决方法
1.设置单元格为文本 $objPHPExcel = new PHPExcel(); $objPHPExcel->setActiveSheetIndex(0); $objPHPExcel-> ...