Duplicate number in array (Java implementation) __java

Source: Internet
Author: User

The title of the sword refers to the offer face question 51


New Ket Network test address: HTTPS://WWW.NOWCODER.COM/QUESTIONTERMINAL/623A5AC0EA5B4E5F95552655361AE0A8


The repetition of the digital heat index in the [programming questions] Array: 40653 time limit: 1 seconds space limit: 32768K All numbers in an array of length n are within the range of 0 to n-1. Some of the numbers in the array are duplicates, but there are several numbers that are not known to be duplicates. Nor do we know how many times each number repeats. Please find any duplicate numbers in the array. For example, if you enter an array {2,3,1,0,2,5,3} with a length of 7, the corresponding output is a duplicate number of 2 or 3.

The Java code is as follows:

Package go.jacob.day529; Parameters://numbers:an array of integers//length:the length of array numbers//duplication: (O Utput) The duplicated number in the array number,length of duplication array is 1,so using duplication[0] =?
in implementation; Here duplication like to Pointor in C + +, duplication[0] equal *duplication in + +//here to pay special attention to return any weight Duplicate one, assigned duplication[0]//return value:true If the input is valid, and there are some duplications in the array numb  ER//Otherwise false public class Demo1 {/* method one: Time complexity O (n), Space complexity O (n) using the principle of hash table non-optimal solution * * Public Boolean duplicate_1 (int numbers[], int length, int[] duplication) {if (numbers = null | | length <= 0) return FA
		Lse You must ensure that the elements in the array are between 0 and n-1 for (int i = 0; i < length; i++) if (Numbers[i] < 0 | | numbers[i] > length) retur

		n false;
		Int[] count = new Int[length];
		for (int i = 0; i < length; i++) {count[i] = 0;

	}	for (int i = 0; i < length; i++) {if (count[numbers[i]] = = 1) {duplication[0] = numbers[i];
			return true;
		} count[numbers[i]]++;
	return false; /* * Method Two: Time complexity O (n), Space complexity O (1) Optimal solution: Although the code has a double loop, but each number can only exchange two times to find its own * position, so the total time complexity is O (n)/public boolean du
		plicate_2 (int numbers[], int length, int[] duplication) {if (numbers = null | | length <= 0) return false;

		for (int i = 0; i < length; i++) if (Numbers[i] < 0 | | | numbers[i] >= length) return false; for (int i = 0; i < length; i++) {while (numbers[i]!= i) {if (numbers[numbers[i)] = = Numbers[i]) {Dupli
					Cation[0] = numbers[i];
				return true;
				//Exchange int tmp = Numbers[i];
				Numbers[i] = numbers[tmp];
			NUMBERS[TMP] = tmp;
	return false;
 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.