Wins the offer (50)

Title: all numbers in an array of length n's are in the range of 0 to n-1. Some digital array is duplicated, but do not know how many numbers are duplicated. Do not know each digit is repeated several times. Please find an array of any one of the duplicate numbers. For example, if the length of the input array 7 {2,3,1,0,2,5,3}, then the corresponding output of the first 2 repeating digits.

Thinking: this question is simple, see the code comments

	public boolean duplicate(int [] numbers, int length, int[] duplication) {
		if (numbers == null || numbers.length == 0) {
			return false;
		}
		// 判断数组是否合法(每个数都在0~n-1之间)
		for (int i = 0; i < length; i++) {
			if (numbers[i] < 0 || numbers[i] > length - 1) {
				return false;
			}
		}
		Arrays.sort(numbers);// 先排序
		int flag = 0;// 做标记
		for (int i = 0; i < length - 1; i++) {
			if (numbers[i] == numbers[i + 1]) {
				duplication[0] = numbers[i];
				flag = 1;
				break;
			}
		}
		return flag == 1 ? true : false;
	}

Published 50 original articles · won praise 0 · Views 378

Guess you like

Origin blog.csdn.net/weixin_46108108/article/details/104218781