剑指offer-38.数组中重复的数(39)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_38332722/article/details/100558442

38.数组中重复的数(39)

  • 题目描述:在一个长度为n的数组里的所有数字都在0到n-1的范围内。
    数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。
    例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。

  • 代码

    package _38.数组中重复的数;
    
    import java.util.Arrays;
    
    public class Duplicate {
    	
    	public static void main(String[] args) {
    		int numbers[] = {2,3,1,0,2,5,3};
    		int duplication[] = new int[1];
    		duplicate3(numbers,7,duplication);
    		System.out.println(duplication[0]);
    	}
    	/**
    	 * 解法一:暴力解决;使用两个循环遍历;不改变原数组的次序。 时间复杂度:O(n^2) 空间复杂度:O(1)
    	 * 
    	 * @param numbers
    	 * @param length
    	 * @param duplication
    	 * @return
    	 */
    	public static boolean duplicate1(int numbers[], int length, int[] duplication) {
    		if (numbers == null || length < 2)
    			return false;
    		for (int i = 0; i < numbers.length; i++) {
    			for (int j = i + 1; j < numbers.length; j++) {
    				if (numbers[i] == numbers[j]) {
    					duplication[0] = numbers[i];
    					return true;
    				}
    			}
    		}
    		return false;
    	}
    
    	/**
    	 * 解法二:将数组排序后再查找重复元素。
    	 *  	  时间复杂度为:O(nlogn)
    	 *       空间复杂度为:O(1)
    	 * 
    	 * @param numbers
    	 * @param length
    	 * @param duplication
    	 * @return
    	 */
    	public static boolean duplicate2(int numbers[], int length, int[] duplication) {
    		if (numbers == null || length < 2)
    			return false;
    		// Arrays.sort(numbers);
    		quickSort(numbers, 0, length - 1);
    		System.out.println(Arrays.toString(numbers));
    		for (int i = 0; i < length; i++) {
    			if(numbers[i] == numbers[i+1]){
    				duplication[0] = numbers[i];
    				return true;
    			}
    		}
    		return false;
    	}
    
    	// 快排
    	public static void quickSort(int arr[], int start, int end) {
    		// 找出当前数组中第一个元素的位置
    		if (start < end) {
    			// 将数组中第一个元素作为基准,找出他的位置
    			int stard = arr[start];
    			// 分别从数组的前后来遍历数组,当low==high的时候遍历完成
    			int low = start;
    			int high = end;
    			while (low < high) {
    				while (arr[high] >= stard && low < high) {
    					high--;
    				}
    				arr[low] = arr[high];
    				while (arr[low] < stard && low < high) {
    					low++;
    				}
    				arr[high] = arr[low];
    			}
    			arr[high] = stard;
    
    			// 处理左边的元素
    			quickSort(arr, start, high - 1);
    			// 处理右边的元素
    			quickSort(arr, high + 1, end);
    		}
    	}
    	
    	/**
    	 * 解法三:由于数组中的元素的大小在[0,n-1]之间,
    	 *        可以用另外一个辅助空间的保存数组中元素是否重复的状态,
    	 *        数组中的元素大小对应辅助空间以该元素为下标的状态。
    	 * @param numbers
    	 * @param length
    	 * @param duplication
    	 * @return
    	 */
    	public static boolean duplicate3(int numbers[], int length, int[] duplication) {
    		if(numbers == null || length < 2) return false;
    		
    		boolean state[] = new boolean[length];
    		for(int i = 0; i < length; i++){
    			if(state[numbers[i]] == true){//再次访问
    				duplication[0] = numbers[i];
    				return true;
    			}
    			state[numbers[i]] = true;//表示number[i]已经访问过
    		}
    		return false;
    	}
    }
    

猜你喜欢

转载自blog.csdn.net/qq_38332722/article/details/100558442