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
文 / 腾讯 吴双 前言 优测小优 有话说: 腾讯优测只有应用测试大神?不不不,我们还有各种研发大牛! *** vim 是一种信仰,我自从2004年有了这个信仰,已经12个年头了.本文介绍了学习vim ...
- 网络延迟查看器 Network latency view 1.4
这是个用于查看网络延迟/ip/主机/地区的工具,内外网通吃,外网可通过这里下载csv以显示国家(地区) 可以自己决定winpcap或者原始套接字进行捕捉 如果只扫描内网推荐angryip 这是款发布在 ...
- abbyy cup a
link: http://codeforces.com/contest/331/problem/A2 /* ID: zypz4571 LANG: C++ TASK: abby_a.cpp */ #in ...
- HDU 1241 Oil Deposits --- 入门DFS
HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...
- codeforces 725/C
Hidden Word time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- c++时间处理
struct tm;这是一个结构体,包括了时间的各个属性年月日,时分秒 time(time_t * t);获取从1900年到现在经过的毫秒数,或者也可以这么用time_t t=time(NULL); ...
- spring 初始化时注入bean实现listener的方法
两种方法: 1.实现ApplicationListener<ContextRefreshedEvent>的onApplicationEvent(ContextRefreshedEvent ...
- 自然语言处理3.3——使用Unicode进行文字处理
全世界有多种语言,经常需要应用程序处理不同的语言和字符集.下面将介绍如何利用Unicode处理使用非ASCII字符集文字. 1.什么是Unicode Unicode支持一百万种以上的字符,每一个字符分 ...
- System.Web.HttpContext.Current 跟踪分析
public static HttpContext Current { get { return ContextBase.Current as HttpContext; } set { Context ...
- for 与 foreach 性能
For 与Foreach 性能 差别在不同的场景下会有不同的差异. 对于不同的目标 , 如 T[] 与 IEnumerable<T> 两个的性能就感觉出来了,对于T[] 都快. ...