《牛客网剑指offer50题》找出数组中任意一个重复的数字
2017-02-27 21:17
405 查看
题目:在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3。
结果:牛客网编译通过
代码及思路:
// 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
//中心思想:不需要新创建数组,遍历数组,把原数组中每个元素的值作为下标index,
//该下标所对应元素的值加上数组长度,如果找到某一数组下标对应元素大于数组长度,
//说明该下标已经存在,该下标就是重复出现的数,时间复杂度O(n)
bool duplicate(int numbers[], int length, int* duplication) {
for(int i=0;i<length;i++){
int index = numbers[i];
if(index>length) //注意构造数组下标时,如果下标大于数组长度先减去数组长度
index -= length; //index为原数组中的元素
if(numbers[index] >= length){
*duplication = index; //该下标对应的元素大于数组长度,说明该下标加过一次数组长度,说明原数组的元素中出现过该下标的值
return true;
}
numbers[index]+=length; //把遍历过的原数组中元素作为下标的值,加上数组长度
}
return false;
}
结果:牛客网编译通过
代码及思路:
// 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
//中心思想:不需要新创建数组,遍历数组,把原数组中每个元素的值作为下标index,
//该下标所对应元素的值加上数组长度,如果找到某一数组下标对应元素大于数组长度,
//说明该下标已经存在,该下标就是重复出现的数,时间复杂度O(n)
bool duplicate(int numbers[], int length, int* duplication) {
for(int i=0;i<length;i++){
int index = numbers[i];
if(index>length) //注意构造数组下标时,如果下标大于数组长度先减去数组长度
index -= length; //index为原数组中的元素
if(numbers[index] >= length){
*duplication = index; //该下标对应的元素大于数组长度,说明该下标加过一次数组长度,说明原数组的元素中出现过该下标的值
return true;
}
numbers[index]+=length; //把遍历过的原数组中元素作为下标的值,加上数组长度
}
return false;
}
相关文章推荐
- 《剑指offer》-请找出数组中任意一个重复的数字
- 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{
- 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3
- 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{
- 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。
- 输入一个递增的数组和一个数字s,找出数组中任意两个数使他们的和正好是这个数字s,如果有任意多对数字的和为s,输出任意一堆数字即可,例如,输入数组[1,2,4,7,11,15]和数字
- 在一个长度为n的数组里的所有数字都在1到n-1的范围内。 有一个数字重复若干次,找出这个数字。
- 取值为[1,n-1] 含n 个元素的整数数组至少存在一个重复数,O(n) 时间内找出其中任意一个重复数
- 问题描述如下: 有2.5亿个整数(这2.5亿个整数存储在一个数组里面,至于数组是放在外存还是内存,没有进一步具体说明); 要求找出这2.5亿个数字里面,不重复的数字的个数; 另外,可用的内存限定为600M; 要求算法尽量高效,最优;
- 156 含n个元素的整数数组至少存在一个重复数,在 O(n)时间内找出其中任意一个重复数
- 开辟一个空房间,随机生成一个有30个数的整形数组,找出其中的重复数字,输出其余数字
- 一个含n个元素的整数数组至少存在一个重复数, 请编程实现,在O(n)时间内找出其中任意一个重复数。
- 计数排序——有一个数组,里面是从1到1,000,000的整数,其中有一个数字出现了两次,你怎么找出那个重复的数字?
- 数组a[N],存放了1至N-1个数,其中某个数重复一次。写一个函数,找出被重复的数字.时间复杂度必须为o(N)
- 数组a[N],存放了N个数,其中某个数重复一次。 写一个函数,找出被重复的数字.时间复杂度必须为o(N)
- 取值为[1,n-1]含n个元素的整数数组,至少存在一个重复数,即可能存在多个重复数,O(n)时间内找出其中任意一个重复数,不使用额外存储空间。
- 数组a[N],存放了1至N-1个数,其中某个数重复一次。写一个函数,找出被重复的数字.时间复杂度必须为o(N)函数原型:
- 数组a[N],存放了1至N-1个数,其中某个数重复一次。写一个函数,找出被重复的数字.时间复杂度必须为o(N)函数原型:
- 有一个长度是101的数组,存在1~100的数字,有一个是重复的,找出重复出来
- (思科笔试)数组a[N],存放了1至N-1个数,其中某个数重复一次。写一个函数,找出被重复的数字.时间复杂度必须为o(N)函数原型: