在一个长度为n的数组里的所有数字都在0~n-1的范围里内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。例如,如果输出长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3。
方法1:首先将输入的数组排序,然后从头到尾扫描排序后的数组,若两相邻的数字相等,则此数字为重复数字。排序一个长度为n的数组需要O()的时间复杂度。
Java代码实现
import java.util.Arrays;
public class Solution {
// Parameters:
// numbers: an array of integers
// length: the length of array numbers
// duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation;
// Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++
// 这里要特别注意~返回任意重复的一个,赋值duplication[0]
// Return value: true if the input is valid, and there are some duplications in the array number
// otherwise false
public boolean duplicate(int numbers[],int length,int [] duplication) {
if(length < 2 || numbers == null){
return false;
}
for(int i = 0; i < length; i++){
if(numbers[i] < 0 || numbers[i] > length - 1)
return false;
}
Arrays.sort(numbers);
for(int i = 0; i < length-1; i++){
if(numbers[i] == numbers[i+1]){
duplication[0] = numbers[i];
return true;
}
}
return false;
}
}
方法2:利用哈希表HashSet解决。从头到尾按顺序扫描数组的每个数字,每扫描到一个数字的时候,都可以用O(1)的时间来判断哈希表里是否包含该数字。若不包含,则将该数字加入到哈希表中;若哈希表中包含该数字,则该数字就是我们要找的重复数字。该方法的时间复杂度为O(n),但它提高时间效率是以大小为O(n)的哈希表为代价,即空间换时间。
Java代码实现
import java.util.HashSet;
public class Solution {
// Parameters:
// numbers: an array of integers
// length: the length of array numbers
// duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation;
// Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++
// 这里要特别注意~返回任意重复的一个,赋值duplication[0]
// Return value: true if the input is valid, and there are some duplications in the array number
// otherwise false
public boolean duplicate(int numbers[],int length,int [] duplication) {
if(length < 2 || numbers == null){
return false;
}
for(int i = 0; i < length; i++){
if(numbers[i] < 0 || numbers[i] > length - 1)
return false;
}
HashSet<Integer> hash = new HashSet<>();
for(int i = 0; i < length; i++){
if(!hash.contains(numbers[i])){
hash.add(numbers[i]);
}
else{
duplication[0] = numbers[i];
return true;
}
}
return false;
}
}
方法3:因为数字都是在0~n-1的范围内,如果数组中不存在重复的数字,那么将数组排好序后数字i出现在下标为i的位置。由于数组中没有重复的数字,所以有些位置存在多个数字,同时有的位置可能没有数字。现在让我们重排这个数组。从头到尾扫描数组中的每个数字,当扫描到下标为i的数字后,判断这个数字(用m表示)是不是等于i。如果等于i,则循环继续扫描下一个数字;如果不是,则将这个数字与第m个数字进行比较,如果它和第m个数字相等,则找到了一个重复的数字;否则,将第i个数字和第m个数字交换位置,把m放到属于它的位置,即第m个位置上。接下来再重复这个过程,直到发现一个重复的数字。
Java代码实现
public class Solution {
// Parameters:
// numbers: an array of integers
// length: the length of array numbers
// duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation;
// Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++
// 这里要特别注意~返回任意重复的一个,赋值duplication[0]
// Return value: true if the input is valid, and there are some duplications in the array number
// otherwise false
public boolean duplicate(int numbers[],int length,int [] duplication) {
if(length < 2 || numbers == null){
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[0] =numbers[i];
return true;
}
int temp = numbers[i];
numbers[i] = numbers[temp];
numbers[temp] = temp;
}
}
return false;
}
}
代码中虽然有一两个循环,但每个数字最多交换两次就能找到属于自己的位置,因此总的时间复杂度是O(n),以上所有的操作都是在输入数组上进行的,不需要分配额外的内存,空间复杂度为O(1)。
方法4:数组大小为n,即length,创建一个大小为n标记数组位置的boolean类型辅助数组,两个数组下标位置一一对应,相当于给输入数组的每个位置加入了一个标记,java里boolean默认值为false,然后遍历输入数组,每遍历一个数组,其在自身数组对应的位置的标记设为true,若再次遍历到的数字的对应的位置的标记为true,则表示这个数字出现过,即为要找的重复数字。此方法的总的时间复杂度是O(n),空间复杂度为O(n).
Java代码实现
public class Solution {
// Parameters:
// numbers: an array of integers
// length: the length of array numbers
// duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation;
// Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++
// 这里要特别注意~返回任意重复的一个,赋值duplication[0]
// Return value: true if the input is valid, and there are some duplications in the array number
// otherwise false
public boolean duplicate(int numbers[],int length,int [] duplication) {
if(length < 2 || numbers == null){
return false;
}
for(int i = 0; i < length; i++){
if(numbers[i] < 0 || numbers[i] > length - 1)
return false;
}
boolean[] k =new boolean[length];
for(int i = 0;i < length; i++){
int index = numbers[i];
if(k[index] == true){
duplication[0] = index;
return true;
}
k[index] = true;
}
return false;
}
}