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 ...
随机推荐
- 高逼格的画图:VIM原来可以这样玩
在Linux上其实并不缺少画图软件(比如yEd等),那么为什么还需要用VIM来画图: 更轻,不需要安装太多东西 更小,就是一些文本,比图片什么的小多了,使用起来也更简单 更有逼格 那么我们该怎么做呢? ...
- 完全搞懂傅里叶变换和小波(1)——总纲<转载>
无论是学习信号处理,还是做图像.音视频处理方面的研究,你永远避不开的一个内容,就是傅里叶变换和小波.但是这两个东西其实并不容易弄懂,或者说其实是非常抽象和晦涩的! 完全搞懂傅里叶变换和小波,你至少需要 ...
- Java字段初始化的规律
class InitializeBookClass { { field=200; } public int field=100; public InitializeBookClass(int valu ...
- ASP.NET Web API与Rest web api(一)
本文档内容大部分来源于:http://www.cnblogs.com/madyina/p/3381256.html HTTP is not just for serving up web pages. ...
- UVa1589 象棋
就是声明两个数组,一个模拟棋局,另一个模拟黑将不能走的位置.不过写得有点长了... #include<iostream> #include<string> using name ...
- hihoCoder挑战赛23
hihoCoder挑战赛23 A.Emulator 题意 给一张图,有\(N(N \le 300)\)个点, 给出任意两点之间的最短路. 求最多可以去掉多少条边,使得任意两点的最短路长度不变. 思路 ...
- Linux驱动设计——字符设备驱动(一)
Linux字符设别驱动结构 cdev结构体 struct cdev { struct kobject kobj; struct module *owner; const struct file_ope ...
- IntelliJ IDEA currently
https://www.jetbrains.com/help/idea/2016.2/creating-a-project-from-scratch.html https://www.jetbrain ...
- understanding checkpoint_completion_target
Starting new blog series – explanation of various configuration parameters. I will of course follow ...
- LESS CSS 框架简介(转)
原文地址:http://www.ibm.com/developerworks/cn/web/1207_zhaoch_lesscss/ 简介 CSS(层叠样式表)是一门历史悠久的标记性语言,同 HTML ...