题意:

  有一个含有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. 所思所想 关于asp.net界面业务分离

    1.体会:使用ASP.NET控件来做前段是有很大的局限性的 2.使用拼接HTML的方式来写代码虽然不符合模式,但是有很大的灵活性 3.如果使用拼接字符串的方式来生成前台的代码,使用NV的话完全可以实现 ...

  2. spring mvc如何获取问号后的url参数

    @RequestMapping(method=RequestMethod.GET) public ModelAndView allUsers(@RequestParam int page){ Mode ...

  3. 如果解决ubuntu tab键不能提示命令

    /bin/sh is symlinked to /bin/dashTo change it, do:sudo rm /bin/shsudo ln -s /bin/bash /bin/sh 原文:htt ...

  4. [示例]NSDictionary编程题-字典的排序应用(iOS5班)

    代码? #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepo ...

  5. js图片轮播图

    /*焦点图*/        var Box='.carousel';//盒子        var Menu=$(Box+' .l_cursor li');//圆点菜单        var Con ...

  6. IT公司100题-19-求Fibonacci数列

    问题描述: 定义Fibonacci数列的定义如下:          /    0                           n=0f(n)=      1                  ...

  7. Problem B 队列

    Description Two bored soldiers are playing card war. Their card deck consists of exactly n cards, nu ...

  8. C++全局变量在多个源代码文件中的使用

    在比较大的项目中,如果需要使用全局变量,那么就需要注意一些全局变量声明.使用不当引起的问题了. 本篇文章主要内容有两个:普通全局变量.静态全局变量.全局常量. 1.普通全局变量:假设我们需要在多个不同 ...

  9. 标签工作区(navtab)

    B-JUI使用标签可以加载其他页面的数据 B-JUI框架的整个工作区部分就是一个navtab组件,本页面位于"#bjui-container"容器内,固定的html结构如下: &l ...

  10. Centos6升级内核2.6到3.x过程

    最近公司有一个应用,安装需要内核版本3.1以后,不得已,需要升级下内核版本: 1. 安装必要依赖 # yum groupinstall "Development Tools" #y ...