题目描述:

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

分析:

方法1:

用map保存每个数字出现的次数。

方法2:

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

代码:

方法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) {
map<int, int> myMap;
for(int i = ; i < length; i++) {
if(myMap[numbers[i]] == ) {
*duplication = numbers[i];
return true;
}
myMap[numbers[i]]++;
}
return false;
}
};

方法2:

 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) {
for(int i = ; i < length; i++) {
int m = numbers[i];
if(m >= length) {
m -= length;
if(numbers[m] == numbers[i]) {
*duplication = m;
return true;
}
}
if(numbers[m] >= length) {
*duplication = m;
return true;
}
numbers[m] += length;
}
return false;
}
};

剑指Offer——数组中重复的数字的更多相关文章

  1. 剑指offer数组中重复的数字

    package 数组; /*在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的. 也不知道每个数字重复几次.请找出数组中任意一个重复的数字. ...

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

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

  3. 剑指offer 数组中重复的数

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

  4. 剑指offer--16.数组中重复的数字

    时间限制:1秒 空间限制:32768K 热度指数:198342 本题知识点: 数组 题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复 ...

  5. 剑指offer 数组中的重复数字

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

  6. 剑指Offer-数组中重复的数字

    package Array; /** * 数组中重复的数字 *在一个长度为n的数组里的所有数字都在0到n-1的范围内. * 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次 ...

  7. 剑指Offer 数组中只出现一次的数字

    题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字.   思路: 因为有2个数字只出现了一次,而其他的数字都是2次,可以通过异或运算,得到最后这2个只 ...

  8. python剑指offer数组中出现次数超过一半的数字

    题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...

  9. 剑指Offer——数组中出现次数超过一半的数字——一题多解

    看题目: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...

随机推荐

  1. Hive使用过程中的坑

    在Hive脚本中如果有 use db; #即使用数据库 最后一定要有一个exit;脚本,退出hive窗口 否则运行到最后,hive无法启动MR任务,只是卡在打印完成hive脚本处. 例子如下: $Hi ...

  2. with as 和update ,Delete,insert

    这个SQL写了很久的时间,感觉pgSQL的很是麻烦. with as 先命名一个表出来,就可以当成临时表用. WITH tmp AS ( SELECT MAX(mgi.inner_cd) AS inn ...

  3. OpenERP 7 picking order 继承需要注意的地方

    stock.picking.out  和 stock.picking.in 都是继承自stock.picking 新添加columns时需要注意,在stock.picking.out和stock.pi ...

  4. at91 uart driver for vxworks

    /* at91UART.c - AT91RM9200 serial driver */ /* Copyright 2003-2004 Coordinate Co., Ltd. */ /* Copyri ...

  5. 0810 smarty

    1.Smarty简介(why,what) why:smarrty 将前端工程师和程序员的工作分开,让前端工程师完成前台页面的工作,程序员完成后台的业务逻辑.what模版引擎是用来将PHP代码和模版页组 ...

  6. Appendix A. Common application properties

    Appendix A. Common application properties Prev  Part X. Appendices  Next URl链接:https://docs.spring.i ...

  7. EasyUI Window和Layout

    我们建立tabs内容. <div class="easyui-window" title="Layout Window" icon="icon- ...

  8. 通过策略接口,Spring 框架是高度可配置的,而且包含多种视图技术

    通过策略接口,Spring 框架是高度可配置的,而且包含多种视图技术,例如 JavaServer Pages( JSP)技术.Velocity.Tiles.iText 和 POI.Spring MVC ...

  9. 考虑下面两个JSP文件代码片断: test1.jsp:

    <HTML> <BODY> <% pageContext.setAttribute(”ten”,new Integer(10));%> //1 </BODY& ...

  10. List<T>转DataTable

    /// <summary> /// 将集合类转换成DataTable /// </summary> /// <param name="list"> ...