LeetCode Find the Duplicate Number 找重复出现的数(技巧)
题意:
有一个含有n+1个元素的数组,元素值是在1~n之间的整数,请找出其中出现超过1次的数。(保证仅有1个出现次数是超过1的数)
思路:
方法一:O(nlogn)。根据鸽笼原理及题意,每次如果<=k的数超过了k个,那么答案必定在[1,k]。可以用二分枚举答案来解决。
bool left(vector<int>& nums,int tar)//是否在左边
{
int cnt=;
for(int i=; i<nums.size(); i++)
if(nums[i]<=tar)
cnt++;
return cnt>tar;
} int findDuplicate(vector<int>& nums)
{
int L=, R=nums.size()-;
while(L<R)
{
int mid=R-(R-L+)/;
if( left(nums,mid)==true ) R=mid;
else L=mid+;
}
return R;
}
AC代码
方法二:O(n)。将数组nums看成是一个链表,next[i]表示点i的后继(0也是一个点,因为0也是下标)。根据题意,此链表必定有且仅有一个简单环存在,这样就类似于Linked List Cycle II ,只是会多余出部分的链,但是这不会影响到这个模型,从0点出发依然存在这样的一个模型,只是环的接口处不会是0而已。要注意两个指针的起始位置,必须保证fast=2*slow。
class Solution {
public:
int findDuplicate(vector<int>& nums)
{
int slow=, fast=nums[];
while(fast!=slow)
{
slow=nums[slow];
fast=nums[nums[fast]];
}
fast=;
slow=nums[slow];
while(fast!=slow)
{
slow=nums[slow];
fast=nums[fast];
}
return fast;
}
};
AC代码
LeetCode Find the Duplicate Number 找重复出现的数(技巧)的更多相关文章
- [LeetCode] 219. Contains Duplicate II 包含重复元素 II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- [LeetCode] 220. Contains Duplicate III 包含重复元素 III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- 287. Find the Duplicate Number 找出数组中的重复数字
[抄题]: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive ...
- [LeetCode] 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
Description: Given an array nums containing n + 1 integers where each integer is between 1 and n (in ...
- Leetcode Find the Duplicate Number
最容易想到的思路是新开一个长度为n的全零list p[1~n].依次从nums里读出数据,假设读出的是4, 就将p[4]从零改成1.如果发现已经是1了,那么这个4就已经出现过了,所以他就是重复的那个数 ...
- [CareerCup] 10.6 Find Duplicate URLs 找重复的URL链接
10.6 You have 10 billion URLs. How do you detect the duplicate documents? In this case, assume that ...
- LeetCode 414. Third Maximum Number (第三大的数)
Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...
- LeetCode 217. Contains Duplicate (包含重复项)
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
随机推荐
- go——搭建Win7下的Go开发环境
1.首先需要下载下载go平台安装包 安装程序 下载地址:https://golang.org/dl/ (墙内下载地址http://www.golangtc.com/download),如果是您的系统是 ...
- POJ 3687 逆序拓扑
额.题目大意:有N个球.编号和重量都是唯一不重复的.然后.给你m个pair a和b,表示编号为a的点一定比编号为b的点轻.然后捏.输出每个点对应的重量.关键是要求.如果有多种可能性的话,输出让序号小的 ...
- 小记:利用递归调用循环寻找MP3文件的方法。
private void findMp3Data(File mp3file) { File[] filelist = mp3file.listFiles(); if (filelist != null ...
- [css]邮件的写法
<style type="text/css"> /* Client-specific Styles */ #outlook a{paddin ...
- HDU 4906 Our happy ending(2014 Multi-University Training Contest 4)
题意:构造出n个数 这n个数取值范围0-L,这n个数中存在取一些数之和等于k,则这样称为一种方法.给定n,k,L,求方案数. 思路:装压 每位 第1为表示这种方案能不能构成1(1表示能0表示不能) ...
- Java:String、StringBuffer和StringBuilder的区别
1 String String:字符串常量,字符串长度不可变.Java中String是immutable(不可变)的. String类的包含如下定义: /** The value is used fo ...
- Python的排序
1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a','have','I' ...
- superobject中 JavaToDelphiDateTime的使用
procedure TForm1.FormCreate(Sender: TObject); var n: TDateTime; i64: Int64; s: Integer; begin Memo1. ...
- Maven 玩 github上的项目
第一步,使用maven创建了一个项目"helloworld",cmd命令如下: @echo offecho [INFO] Generating project in ./gener ...
- as3基础知识
在AS3中,值类型数据(简单类型:Boolean.int.Number.String.uint)和引用类型数据(复杂类型)都是 对象,所以这两种类型对象存储的都是引用.但是,对应值类型数据,是一种不变 ...