题意:

  有一个含有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 找重复出现的数(技巧)的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 287. Find the Duplicate Number 找出数组中的重复数字

    [抄题]: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive ...

  4. [LeetCode] Find the Duplicate Number 寻找重复数

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  5. LeetCode——Find the Duplicate Number

    Description: Given an array nums containing n + 1 integers where each integer is between 1 and n (in ...

  6. Leetcode Find the Duplicate Number

    最容易想到的思路是新开一个长度为n的全零list p[1~n].依次从nums里读出数据,假设读出的是4, 就将p[4]从零改成1.如果发现已经是1了,那么这个4就已经出现过了,所以他就是重复的那个数 ...

  7. [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 ...

  8. 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 ...

  9. LeetCode 217. Contains Duplicate (包含重复项)

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

随机推荐

  1. java 访问活动目录代码

    package demo; import java.util.Hashtable; import javax.naming.Context; import javax.naming.NamingEnu ...

  2. SqlServer 慢查询分析优化

    分三步: 记录慢查询的语句到日志文件 1.首先在SSMS,工具菜单下打开Profiler. 2.输入你用户名密码登陆. 3.常规,勾选保存到文件,选择一个文件路径,设置文件大小,这样可以分文件存储日志 ...

  3. IT公司100题-14-排序数组中和为给定值的两个数字

    问题描述: 输入一个升序排序的数组,给定一个目标值target,求数组的两个数a和b,a+b=target.如果有多个组合满足这个条件,输出任意一对即可. 例如,输入升序数组[1, 3, 4, 5, ...

  4. Servlet初识

    1.servlet的生命周期 servlet生命周期中的三大重要时刻 servlet从不存在状态迁移到初始化状态(能够为客户提供服务),首先是从构造函数开始,但是构造函数只是使其成为一个对象,而不是一 ...

  5. 新版的tomcat里面get请求通过http协议的时候似乎默认是UTF-8的编码了吗?

    不在servler.xml的connector中添加URICoding=“UTF-8”,使用默认值一样没有乱码,而添加URICoding=“iso-8859-1”就是乱码了. POST请求还是用iso ...

  6. xcode 5.0 以上去掉icon高亮方法&iOS5白图标问题

    之前的建议方法是把在xxx.info.plist文件中把 icon already includes gloss and bevel effects 设置YES 在Xcode5下,反复实现不成功,今天 ...

  7. linux学习笔记4:linux的任务调度,进程管理,mysql的安装和使用,ssh工具的使用,linux网络编程

    1.设置任务调度命令crontab 任务调度是指系统在某个时间执行的特定的命令或程序.任务调度分为:1)系统工作:有些重要的工作必须周而复始的执行,如病毒扫描.2)个别用户工作:个别用户可能希望执行某 ...

  8. POJ 3156 - Interconnect (概率DP+hash)

    题意:给一个图,有些点之间已经连边,现在给每对点之间加边的概率是相同的,问使得整个图连通,加边条数的期望是多少. 此题可以用概率DP+并查集+hash来做. 用dp(i,j,k...)表示当前的每个联 ...

  9. C语言:typedef 跟 define 的区别

    typedef (int*) pINT1;以及下面这行:#define pINT2 int* pINT1 a,b; 与pINT2 a,b; 定义的a,b 有差别吗 回答: typedef作为类型定义关 ...

  10. 解决mac eclipse 异常退出后无法打开处于loading状态

    <workspace>\.metadata\.plugins\org.eclipse.core.resources目录,删除文件 .snap