The sword refers to offer--51. Repeated numbers in the array

Question: In an array of length n, all numbers are in the range of 0 to n-1. Some numbers in the array are repeated, but I don't know if any number is repeated, and I don't know that each number is repeated Several times, please find any repeated number in the array, eg, input an array of length 7 a={2,3,1,0,2,5,3}, then the corresponding output is the repeated number 2 or 3

Analysis: Several approaches may be considered

1. Sort the input array first, and find the repeated numbers from the sorted array, that is, scan the sorted array from the beginning to the end, and the time complexity of sorting an array of length n is O(nlogn)

2. Hash table, scan each number in the array sequentially from the beginning to the end, and each time a number is scanned, O(1) time can be used to determine whether the number is already contained in the hash table. If the number is not there yet, add it to the hash table, if there is, find a duplicate number, the time complexity is O(n), but at the cost of O(n) space

3. Scan the numbers in the array from the beginning to the end. When the number with the subscript i is scanned, first compare whether the number (m) is equal to i, if it is, then scan the next number, if not, take it and the first number. M numbers are compared, if it is equal to the mth number, a duplicate number is found (the number appears in both subscripts i and m), if it is not equal to the mth number, put Swap the i-th number with the m-th number, put m in its place, and then repeat the comparison and exchange process. The time complexity is O(n) and the space is O(1)

import java.util. *;

public class wr51duplicate {
	public static int duplicate(int numbers[],int length){
		if(numbers==null || numbers.length==0){
			return -1;
		}
		Arrays.sort(numbers);
		int flag=0;
		int duplication=0;
		for(int i=0;i<length-1;i++){
			if(numbers[i]==numbers[i+1]){
				duplication=numbers[i];
				flag=1;
				break;
			}
		}
		return duplication;
	}
	
	public static int duplicate2(int numbers[],int length){
		if(numbers==null || numbers.length==0){
			return -1;
		}
		HashSet<Integer> hs=new HashSet<>();
		int duplication=0;
		for(int i=0;i<length;i++){
			if(!hs.add(numbers[i])){
				duplication=numbers[i];
				break;
			}
		}
		return duplication;
	}
	
	public static boolean duplicate(int numbers[],int length,int [] duplication){
		if(numbers==null || numbers.length==0){
			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;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int []a={2,3,1,0,2,5,3};
		int []result=new int[1];
		System.out.println(duplicate(a,a.length));
		System.out.println(duplicate2(a,a.length));
		System.out.println(duplicate(a,a.length,result));
	}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325601105&siteId=291194637
Recommended