Skip to content

Instantly share code, notes, and snippets.

@necusjz
Last active April 25, 2019 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save necusjz/8aa1c6ddebdd9da390161900a1f6240d to your computer and use it in GitHub Desktop.
Save necusjz/8aa1c6ddebdd9da390161900a1f6240d to your computer and use it in GitHub Desktop.
面试题 3.1:找出数组中重复的数字
bool duplicate(int numbers[], int length, int *duplication) {
if(numbers == nullptr || length <= 0) {
return false;
}
for(int i = 0; i < length; ++i) {
if(numbers[i] < 0 || numbers[i] > length-1) {
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment