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 = 0; i < length; i++){
//由于数字都是0到length -1范围内的,将以遍历到的数字为下标的元素加上length
//当第二 次找到该下标元素,发现它大于length-1时,说明,该下标数字重复了。
//当遍历到一个元素,发现它大于length -1时,让它前去length,就为原来的元素
int index = numbers[i];//当遍历到一个元素,发现它大于length -1时,让它前去length,就为原来的元素
if(index > length - 1)
index -= length;
if(numbers[index] > length - 1){//当第二 次找到该下标元素,发现它大于length-1时,说明,该下标数字重复了。
*duplication = index;
return true;
}
numbers[index] += length;//由于数字都是0到length -1范围内的,将以遍历到的数字为下标的元素加上length
}
return false;
}
};
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 = 0; i < length; i++){
//由于数字都是0到length -1范围内的,将以遍历到的数字为下标的元素加上length
//当第二 次找到该下标元素,发现它大于length-1时,说明,该下标数字重复了。
//当遍历到一个元素,发现它大于length -1时,让它前去length,就为原来的元素
int index = numbers[i];//当遍历到一个元素,发现它大于length -1时,让它前去length,就为原来的元素
if(index > length - 1)
index -= length;
if(numbers[index] > length - 1){//当第二 次找到该下标元素,发现它大于length-1时,说明,该下标数字重复了。
*duplication = index;
return true;
}
numbers[index] += length;//由于数字都是0到length -1范围内的,将以遍历到的数字为下标的元素加上length
}
return false;
}
};