287 Find the Duplicate Number 寻找重复数
一个长度为 n + 1 的整形数组,其中的数字都在 1 到 n 之间,包括 1 和 n ,可知至少有一个重复的数字存在。假设只有一个数字重复,找出这个重复的数字。
注意:
不能更改数组内容(假设数组是只读的)。
只能使用恒定的额外空间,即要求空间复杂度是 O(1) 。
时间复杂度小于 O(n2)
数组中只有一个数字重复,但它可能不止一次重复出现。
详见:https://leetcode.com/problems/find-the-duplicate-number/description/
方法一:
class Solution {
public:
int findDuplicate(vector<int>& nums) {
int left=1,right=nums.size()-1;
while(left<right)
{
int mid=left+(right-left)/2;
int cnt=0;
for(int val:nums)
{
if(val<=mid)
{
++cnt;
}
}
if(cnt<=mid)
{
left=mid+1;
}
else
{
right=mid;
}
}
return left;
}
};
方法二:
class Solution {
public:
int findDuplicate(vector<int>& nums) {
int s=0,f=0,t=0;
while(true)
{
s=nums[s];
f=nums[nums[f]];
if(s==f)
{
break;
}
}
while(true)
{
s=nums[s];
t=nums[t];
if(s==t)
{
break;
}
}
return s;
}
};
参考:https://www.cnblogs.com/grandyang/p/4843654.html
287 Find the Duplicate Number 寻找重复数的更多相关文章
- [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] Find the Duplicate Number 寻找重复数
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- 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 ...
- 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 287. Find the Duplicate Number (python 判断环,时间复杂度O(n))
LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...
- 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 ...
随机推荐
- HDU 5950 Recursive sequence 递推转矩阵
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- spring依赖注入中获取JavaBean
一.这个接口有什么用? 当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean.换句话说,就是这个类可以 ...
- JDBC的事务
以下内容引用自http://wiki.jikexueyuan.com/project/jdbc/transactions.html: 如果JDBC连接是处于自动提交模式下,该模式为默认模式,那么每句S ...
- Errors running builder 'JavaScript Validator' on
eclipse编译提示Errors running builder 'JavaScript Validator' on 解决方法见下图 去掉 'JavaScript Validator' 即可
- Zookeeper学习 & Paxos
首先,Zookeeper是基于Paxos来进行分布式选举管理的,Paxos的内容可以参考我另一篇文章:http://www.cnblogs.com/charlesblc/p/6037004.html ...
- kafka-manager 的编译和使用(附安装包)
kafka-manager 的编译和使用(附安装包) 学习了:https://my.oschina.net/wangjiankui/blog/653139
- excel2010英文大写怎么变小写
excel大写转小写步骤如下: 1.如下图所示,若要对字母进行大.小写转换可以使用如下的步骤. 2.若要将单元格中的所有字母全部转换为大写形式,可以使用UPPER函数. 公式为 =UPPER(A2) ...
- 自己动手写shell命令之ls -R1fF
ls命令的R參数代表递归的列出全部子目录中的全部文件,1表示每一行仅仅显示一个文件或目录,f表示不排序即输出.F表示在每项的输出的最后依据其文件类型对应的加上*/=>@|字符.通过c语言实现ls ...
- 下载的php_yal.dll文件添加到php的ext
下载的php_yal.dll文件添加到php的ext http://pecl.php.net/package/yaf/3.0.6/windows yaf. use_namespace=1 ;;exte ...
- 预载入和javascript对象
请参见 http://www.west263.com/info/html/wangyezhizuo/Javascript/20080225/34168.html