数组中重复的数字
2016-09-02 11:23
162 查看
在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3。
import java.util.*; public class Solution { // Parameters: // numbers: an array of integers // length: the length of array numbers // duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation; // Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++ // 这里要特别注意~返回任意重复的一个,赋值duplication[0] // Return value: true if the input is valid, and there are some duplications in the array number // otherwise false public boolean duplicate(int numbers[],int length,int [] duplication) { if (numbers == null || length <= 0) return false; Set<Integer> set = new HashSet<Integer>(); for (int i=0; i<length; i++) { if (!set.add(numbers[i])) { duplication[0] = numbers[i]; return true; } } return false; } }
相关文章推荐
- 【我解C语言面试题系列】009 特殊的去除数组中重复数字问题
- 数组a[N],存放了1至N-1个数,其中某个数重复一次。写一个函数,找出被重复的数字.时间复杂度必须为o(N)函数原型:
- 厦门1165 第K小数 树状数组 不重复数字
- 判断一个整数数组中是否有重复数字出现的O(n)时间复杂度算法
- javascript数字数组去重复项
- 【我解C语言面试题系列】008 去除数组中重复数字问题
- java 一个int数组 长度为100 随机生成100个数 即1-100 将其插入进数组 插入的数字不能重复
- ASP 无重复数字随机函数, 数组实现, 并应用于随机显示记录集 By shawl.qiu
- 删除数组中重复的数字
- 有一个数组,存储的元素为1到10000000的任意数,在其中查找出一个重复的数字
- 数组a[N],存放了1至N-1个数,其中某个数重复一次。写一个函数,找出被重复的数字.时间复杂度必须为o(N)函数原型:
- JAVA去掉一个已排序数组的重复数字
- 去除数组中的重复数字
- 判断数组中是否包含重复数字
- 特殊的去除数组中重复数字问题
- 【我解C语言面试题系列】009 特殊的去除数组中重复数字问题
- 去除数组中的重复数字
- 软件开发者面试百问-----有一个数组,里面是从1到1,000,000的整数,其中有一个数字出现了两次,你怎么找出那个重复的数字?
- 有个数组中有100w个数,其中有一个数重复了50w次,要求找出这个数字
- 可用的ASP无重复数字随机函数, 数组实现, 并应用于随机显示记录集