算法(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 ...
随机推荐
- 洛谷P3804 【模板】后缀自动机
题目描述 给定一个只包含小写字母的字符串 SS , 请你求出 SS 的所有出现次数不为 11 的子串的出现次数乘上该子串长度的最大值. 输入输出格式 输入格式: 一行一个仅包含小写字母的字符串 SS ...
- ubuntu系统下的docker
官网:https://www.docker.com/ 相关资料:1.Docker入门教程 http://dockone.io/article/1112.Docker_百度百科 http://baike ...
- 实现高可用-Keepalived
简介 Keepalived是HA Cluster(High Availability Cluster,高可用集群)的一个服务软件,用来防止单点故障. Keepalived采用VRRP(virtual ...
- 配置p6spyLog输出sql完整日志
第一步: 配置maven <dependency> <groupid>p6spy</groupid> <artifactid>p6spy< ...
- 【mysql处理远程登陆授权及数据库迁移备份问题】
Database changedMariaDB [mysql]> grant all PRIVILEGES on mysql.* to root@'%' identified by '123'; ...
- Hive初识(二)
Hive分区 Hive组织表到分区.它是将一个表到基于分区列,如日期,城市和部门的值相关方式.使用分区,很容易对数据进行部分查询. 表或分区是细分成桶,以提供额外的结构,可以使用更高效的查询的数据.桶 ...
- java服务端项目开发规范
更新内容 2015-03-13 (请先更新svn的mybatis.xml.BaseMapper.java.Pager.java文件) 加入测试类规范 加入事物控制规范 加入mapper接口规则 ...
- OMAPL138制作SD卡启动介质及重装Linux系统
OMAPL138制作SD卡启动盘及重装Linux系统 手里的创龙的OMAPL138平台的系统SSH坏掉了,我重新移植了openssh还是不好使,没有办法了只能重装OMAPL138的系统了,按照创龙给的 ...
- (数据科学学习手札20)主成分分析原理推导&Python自编函数实现
主成分分析(principal component analysis,简称PCA)是一种经典且简单的机器学习算法,其主要目的是用较少的变量去解释原来资料中的大部分变异,期望能将现有的众多相关性很高的变 ...
- Linux篇:因为修改了/etc/sudoers 文件的权限导致的问题
因为想要把sudo变成免密码所以就查了网上的教程.说是要修改/etc/sudoers文件,但是修改的时候发现这个文件是只读, 所以就 /etc/sudoers 结果就导致了接下来用sudo的时候提示如 ...