287. Find the Duplicate Number
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:
- You must not modify the array (assume the array is read only).
- You must use only constant, O(1) extra space.
- Your runtime complexity should be less than
O(n2)
. - 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的更多相关文章
- 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 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 ...
- LeetCode 287. Find the Duplicate Number (python 判断环,时间复杂度O(n))
LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...
- [LeetCode] 287. 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 ...
- LeetCode 287. 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 找出数组中的重复数字
[抄题]: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive ...
- 【LeetCode】287. Find the Duplicate Number
Difficulty:medium More:[目录]LeetCode Java实现 Description Given an array nums containing n + 1 integer ...
- [LeetCode] 287. Find the Duplicate Number(Floyd判圈算法)
传送门 Description Given an array nums containing n + 1 integers where each integer is between 1 and n ...
随机推荐
- coreseek(sphinx)安装1(xml数据源配置和测试)
1.下载coreseek-3.2.14-32版本.网址:http://www.coreseek.cn/products-install/install_on_windows/ (有详细的安装说明) ...
- JAVA 23种设计模式(转)
1.FACTORY?追MM少不了请吃饭了,麦当劳的鸡翅和肯德基的鸡翅都是MM爱吃的东西,虽然口味有所不同,但不管你带MM去麦当劳或肯德基,只管向服务员说“来四个鸡翅”就行了.麦当劳和肯德基就是生产鸡翅 ...
- SQL注入测试平台 SQLol -2.SELECT注入测试
前面,我们已经安装好了SQLol,打开http://localhost/sql/,首先跳转到http://localhost/sql/select.php,我们先从select模块进行测试. 一条完成 ...
- Linux 安装rar解压工具
下载RAR安装包: http://www.rarsoft.com/download.htm 我的是CentOS 64位: wget http://www.rarsoft.com/rar/rarlinu ...
- Python学习(5)——内置函数
常用字符串内置函数 1)str.count() //返回该字符串中某个子串出现的次数 2)str.find() //返回某个子串出现在该字符串的起始位置 3)str.lower() //将该字符串全部 ...
- MySQL中日期与字符串相互转换,并进行日期比较查询
技术交流群:233513714 1.日期无需转换查询(日期在数据库中的类型为字符串) select * from day where dateTime > '2016-03-15' 2.使用da ...
- 【NOIP2013】华容道
看别人的代码然后被坑了一下午+一晚上,睡一觉第二天醒悟过来打表过了 果然打表才是正确的调试方法,跟踪什么的去屎(╯‵□′)╯︵┻━┻ 原题: 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成 ...
- 修改host文件屏蔽视频广告和网站
很多时候我们会需要屏蔽一些网站或者广告,类似XX网站,下木马病毒的网站,或者破解软件的时候.我们可以使用一些软件屏蔽,我这里是用windows系统自带的hosts文件来屏蔽的.这个文件有点类似精简版的 ...
- Performance Analysis Methodology
http://www.brendangregg.com/methodology.html The USE Method: for finding resource bottlenecks The TS ...
- nginx和apache配置目录浏览功能
今天工作需要,要给客户提供一个patch的下载地址,于是想用nginx的目录浏览功能来做,需要让客户看到指定一个目录下的文件列表,然后让他自己来选择该下载那个文件: 我们都知道在apache下可以配置 ...