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-1;i++)
{
//判断当前数是否和下标相符
if(numbers[i]!=i)
{
//如果和下标不相符,并且原下标已经有复合下标的数。就表示这个数重复了。
if(numbers[i] == numbers[numbers[i]])
{
//将重复的数传入指针。
*duplication =numbers[numbers[i]];
return true;
}
//交换重复的数。
swap(numbers[i],numbers[numbers[i]]);
}
}
return false;
}
};
【offer7】数组中重复的数。
最新推荐文章于 2022-05-13 11:15:15 发布