在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。
2017-12-09 21:17
1571 查看
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<
4000
br />public boolean duplicate(int numbers[],int length,int [] duplication) {
boolean flag=false;
boolean flag_duplication=true;
if(length <=1 || numbers==null){
return flag;
}
//使用基数排序
for(int i=0;i<numbers.length;i++){
while(numbers[i] !=i){
if(numbers[numbers[i]]==numbers[i]){
//找到相同的数字
duplication[0]=numbers[i];
flag=true;
return flag;
}
//交换
int temp=numbers[i];
numbers[i]=numbers[temp];
numbers[temp]=temp;
}
}
return flag;
}
}
// 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<
4000
br />public boolean duplicate(int numbers[],int length,int [] duplication) {
boolean flag=false;
boolean flag_duplication=true;
if(length <=1 || numbers==null){
return flag;
}
//使用基数排序
for(int i=0;i<numbers.length;i++){
while(numbers[i] !=i){
if(numbers[numbers[i]]==numbers[i]){
//找到相同的数字
duplication[0]=numbers[i];
flag=true;
return flag;
}
//交换
int temp=numbers[i];
numbers[i]=numbers[temp];
numbers[temp]=temp;
}
}
return flag;
}
}
相关文章推荐
- 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{
- 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{
- 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3
- 在一个长度为n的数组里的所有数字都在1到n-1的范围内。 有一个数字重复若干次,找出这个数字。
- 面试题:给定一个长度为N的数组,其中每个元素的取值范围都是1到N。判断数组中是否有重复的数字
- 给定一个长度为N的数组,其中每个元素的取值范围都是1到N。判断数组中是否有重复的数字。(原数组不必保留)
- 一个长度为N的整形数组,数组中每个元素的取值范围是[0,N-1],写一个算法判断数组中是否存在重复的数字
- 给定一个数组,当中有正负数,求当中的一段“子数组”(即任意长度,连续的数字), 使得这个“子数组”的和是所有“子数组”和中最大的
- 《牛客网剑指offer50题》找出数组中任意一个重复的数字
- 《剑指offer》-请找出数组中任意一个重复的数字
- 有一个长度是101的数组,存在1~100的数字,有一个是重复的,找出重复出来
- randnumber_mn.c 生成一个数组,包括k个不重复的整数,并且要求这些整数范围为[m,n),生成的结果中不能包含inum中的数字,size表示inum的长度
- 一个存在重复数字的正整数数列,每个数都不超过32,找出其中所有重复的数字
- 一个数组arr,长度为N,N小于10^8,里面数字的范围是1~N,是否有重复,并统计出来
- 1、在一个由自然数1-1000中某些数字所组成的数组中,每个数字可能出现零次或者多次。设计一个算法,找出出现次数最多的数字。
- 一个数组arr,长度为N,N小于10^8,里面数字的范围是1~N,求每个数字出现的次数
- 输入一个递增的数组和一个数字s,找出数组中任意两个数使他们的和正好是这个数字s,如果有任意多对数字的和为s,输出任意一堆数字即可,例如,输入数组[1,2,4,7,11,15]和数字
- 数组a[N],存放了1至N-1个数,其中某个数重复一次。写一个函数,找出被重复的数字.时间复杂度必须为o(N)函数原型:
- 取值为[1,n-1] 含n 个元素的整数数组至少存在一个重复数,O(n) 时间内找出其中任意一个重复数
- 长度为n的数组,有一个数重复出现了n/2+1次,找出(三种方法)