题目描述

在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3。

【思路1】需要一个容器来存储暂时没有重复的数字,在向后遍历的过程中如果在容器中找到相同数字则返回。

 class Solution {
public:
// Parameters:
// numbers: an array of integers
// length: the length of array numbers
// duplication: (Output) the duplicated number in the array number
// Return value: true if the input is valid, and there are some duplications in the array number
// otherwise false
bool duplicate(int numbers[], int length, int* duplication) {
if(numbers == NULL) return false;
vector<int> vec;
int count = ;
for(int i = ;i < length;i ++){
if(numbers[i] < || numbers[i] > length - ) return false;
if(find(vec.begin(),vec.end(),numbers[i]) != vec.end()){
duplication[] = numbers[i];
return true;
}else{
vec.push_back(numbers[i]);
}
}
if(duplication[] == -) return false;
else return true;
}
};

【思路2】不需要额外的数组来保存,题目里写了数组里数字的范围保证在0 ~ n-1 之间,所以可以利用现有数组设置标志,当一个数字被访问过后,可以设置对应位上的数 + n,之后再遇到相同的数时,会发现对应位上的数已经大于等于n了,那么直接返回这个数即可。

 class Solution {
public:
// Parameters:
// numbers: an array of integers
// length: the length of array numbers
// duplication: (Output) the duplicated number in the array number
// Return value: true if the input is valid, and there are some duplications in the array number
// otherwise false
bool duplicate(int numbers[], int length, int* duplication) {
if(numbers == NULL) return false;
for(int i = ;i < length;i ++){
int index = numbers[i];
if(index >= length){
index -= length;
}
if(numbers[index] >= length){
duplication[] = index;
return true;
}
numbers[index] += length;
}
return false;
}
};

[剑指Offer] 50.数组中重复的数字的更多相关文章

  1. 剑指Offer 50. 数组中重复的数字 (数组)

    题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...

  2. 剑指 Offer 03. 数组中重复的数字

    剑指 Offer 03. 数组中重复的数字 找出数组中重复的数字. 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知 ...

  3. 菜鸟刷题路:剑指 Offer 03. 数组中重复的数字

    剑指 Offer 03. 数组中重复的数字 哈希表/set class Solution { public int findRepeatNumber(int[] nums) { HashSet< ...

  4. 5.1 剑指 Offer 03. 数组中重复的数字

    类型题:剑指 Offer 03. 数组中重复的数字 找出数组中重复的数字.在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了, ...

  5. 【剑指Offer】数组中重复的数字 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 Set 快慢指针 日期 题目地址:https://leetcod ...

  6. Go语言实现:【剑指offer】数组中重复的数字

    该题目来源于牛客网<剑指offer>专题. 在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组 ...

  7. (python)剑指Offer:数组中重复的数字

    问题描述 在长度为n的数组中,所有的元素都是0到n-1的范围内. 数组中的某些数字是重复的,但不知道有几个重复的数字,也不知道重复了几次,请找出任意重复的数字. 例如,输入长度为7的数组{2,3,1, ...

  8. 剑指offer:数组中重复的数字

    题目描述: 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度 ...

  9. 【剑指offer】数组中重复的数字

    题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...

随机推荐

  1. 分享Centos6.5升级glibc过程

    默认的Centos6.5 glibc版本最高为2.12, 而在进行Nodejs开发时项目所依赖的包往往需要更高版本的glibc库支持, 因此在不升级系统的前提下, 需要主动更新系统glibc库. 一般 ...

  2. IP数据报、TCP报文、UDP报文格式

    总是记不得TCP/IP协议的各个协议格式,特在此做个记录,好方便回顾. 信息来自众多网络大神们的总结,我再结合自己的理解整理所得. ================================== ...

  3. 20145226夏艺华 逆向及Bof基础实践

    逆向及Bof基础实践 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串,如图所示: 该程序同时 ...

  4. day 4 飞机大战-面向对象

    1.飞机类 #-*- coding:utf-8 -*- import pygame import time from pygame.locals import * class HeroPlane(ob ...

  5. jquery 点滴

    jQuery——动态给表格添加序号 $(function(){ //$('table tr:not(:first)').remove(); var len = $('table tr').length ...

  6. facebook原生广告添加adchoice图标

    1.在需要显示adchoice的地方添加一个textview: <LinearLayout android:id="@+id/ad_ic_action" android:la ...

  7. uvaoj1586Molar mass(暴力)

    An organic compound is any member of a large class of chemicalcompounds whose molecules contain carb ...

  8. 内置方法(item系列)

    class Foo: def __init__(self,name): self.name = name def __getitem__(self, item): # 获取时触发 print('get ...

  9. android AVD创建

    参数详解:AVD name:是要填写的虚拟机名称,这个自己随便取名就行了,要纯英文和数字组成Device:这里是要选择模拟的设备,一般选择3.2*QVGA(ADP2)(320*480: mdpi)这个 ...

  10. vim常用命令—撤销与反撤销

    命令模式下(即按ESC后的模式) u 撤销 Ctrl r (组合键) 反撤销<后悔撤销>