题意:

  有一个含有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面试题(1)- 高级特性

    1. The diffrence between java.lang.StringBuffer and java.lang.StringBuilder? java.lang.StringBuffer: ...

  2. li排序的两种方法

    1.一般做法 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <ti ...

  3. K需要修改的内容

    1.需要保存默认案件,所有相关的页面的Title都要显示默认按键信息. 2.播放器需要调整,左侧的是播放信息,用户选择:案件/设备/然后就把该目录下的文件都展示出来.用户选择的时候马上进行播放.右侧有 ...

  4. AFNetworking框架使用

    本文是由 iOS Tutorial 小组成员 Scott Sherwood撰写,他是一个基于位置动态加载(Dynamically Loaded)的软件公司(专业的混合定位)的共同创办人. 网络 — 你 ...

  5. linux下不能使用shutdown命令

    命令查看:  #echo $PATH     /usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/sbin;/ ...

  6. AudioManager音频管理器

    AudioManager音频管理器提供了如下几种常用方法来控制手机音频: 1.adjustStreamVolume(int StreamType,int direction,int flgs):调整手 ...

  7. javascrip自定义对象的方式

    对象初始化方式(也叫json对象创建方式) <script type="text/javascript"> var User = { name:"paul&q ...

  8. 一模 (6) day2

    第一题: 题目大意:求最长公共上升子序列(LICS): 解题过程: 1.一开始想到模仿求最长公共子序列的方法,F[i][j]表示A串前i个,B串前j个的最长公共子序列,很明显当A[i]!= B[j]时 ...

  9. 基于K2 BPM的大型连锁企业开关店选址管理解决方案

    业内有句名言:“门店最重要的是什么?第一是选址,第二是选址,第三还是选址” 选址是一个很复杂的综合性商业决策过程,需要定性考虑和定向分析.K2开关店&选址管理方案重点关注:如何开出更好的店?在 ...

  10. [转] lib和dll 区别,生成及使用方法

    lib 和 dll 的区别.生成以及使用详解 [目录] lib dll介绍 生成动态库 调用动态库 生成静态库 调用静态库 首先介绍一下静态库(静态链接库).动态库(动态链接库)的概念,首先两者都是代 ...