# 03_1_FindDuplication（数组中重复的数字）

## 题目描述

position-0 : (2,3,1,0,2,5) // 2 <-> 1
(1,3,2,0,2,5) // 1 <-> 3
(3,1,2,0,2,5) // 3 <-> 0
position-1 : (0,1,2,3,2,5) // already in position
position-2 : (0,1,2,3,2,5) // already in position
position-3 : (0,1,2,3,2,5) // already in position
position-4 : (0,1,2,3,2,5) // nums[i] == nums[nums[i]], exit

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) {

if (numbers == nullptr || length <= 0)
return false;

for (int i = 0; i < length; ++i)
{
while (numbers[i] != i)
{
if (numbers[i] == numbers[numbers[i]])
{
*duplication = numbers[i];
return true;
}

int temp = numbers[i];
numbers[i] = numbers[temp];
numbers[temp] = temp;
}

}

return false;

}
};
int main()
{
int num[] = { 2,3,1,0,2,5,3 };
Solution sol;
int p[] = { 0 };
bool b = sol.duplicate(num, 7, p);
cout << b << endl;
cout << *p << endl;
return 0;

}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120