算法(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 ...
随机推荐
- leetcode算法之 Single Number
题目描述: 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 要求: 时间复杂度O(n),空间复杂都O(1) 示例: 输入: [2,2,1] 输 ...
- chromium之tracked_objects
// For each thread, we have a ThreadData that stores all tracking info generated // on this thread. ...
- Intellij IDEA切换maven
问题描述: IDEA自带Maven,但不想用,想用自己安装的. 解决方案: File->Settings(快捷键:Ctrl+Alt+S) 这里分为了两个,竟然还有默认配置一说,上面的只是修改了当 ...
- MySQL5.7版本安装
安装方式一: ZIP压缩包安装 >>>首先,到MYSQL官网下载.zip格式的MySQL Server的压缩包,根据需要选择x86或x64版. >>>下载需要登录o ...
- 仿制用友U8界面
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- 插头DP(基于连通性状态压缩的动态规划问题)(让你从入门到绝望)
今天,我,Monkey king 又为大家带来大(ju)佬(ruo)的算法啦!--插头DP 例题(菜OJ上的网址:http://caioj.cn/problem.php?id=1489): 那么,这道 ...
- [拉格朗日反演][FFT][NTT][多项式大全]详解
1.多项式的两种表示法 1.系数表示法 我们最常用的多项式表示法就是系数表示法,一个次数界为\(n\)的多项式\(S(x)\)可以用一个向量\(s=(s_0,s_1,s_2,\cdots,s_n-1) ...
- 【娱乐向】制作Chrome天气预报扩展程序
1.什么是Chrome扩展程序 Chrome扩展程序是一个用Web技术开发,用来扩展增强浏览器功能的软件.和一般的网页一样,Chrome扩展程序由html.js.css和图片等部分组成.Chrome插 ...
- Ubantu E325 错误的解决办法
这个错误是由于未正确关闭文本编辑器导致的: 图一 解决办法:退出后输入 sudo vim /etc/hosts 图二 直接输入 D 即可! 注意:退出文本编辑器的正确方式是:ESC键 + :wq. 我 ...
- Apache 服务器性能评估
1 查看当前并发连接数 netstat -an | grep ESTABLISHED | wc -l 2 查看当前进程数 ps aux|grep httpd|wc -l