Title: 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 repeated, but I don't know how many numbers are repeated. And I don't know the number of times each number repeats. Please find a random number in the array.
For example, assuming that the input length is 7 of the array {2,3,1,0,2,5,3}, then the corresponding output is repeated the number 2 or 3.
An easy way to solve the problem is to sort the input arrays first. It is easy to find repeated numbers from a sorted array, simply by scanning the sorted array from beginning to end. Sorting an array of length n requires a time of O (Nlogn) time.
You can also use a hash table to solve the problem. Scans each number of arrays sequentially from beginning to end. Each time a number is scanned, it is possible to use O (1) to infer whether the hash table has included the number. Suppose the hash table does not have this number. Just add it to the hash table.
Assuming that the number already exists in the hash table, a repeating number is found.
The time complexity of this algorithm is O (n), but it increases the time complexity at the cost of a hash table of size O (n). Let's see if there is an algorithm with a time complexity of O (1).
We notice that the numbers in the array are within the range of 0 to n-1. Suppose there are no repeated numbers in this array. Then when the array is sorted, the number I will be the position of the subscript I now. Because there are repeated numbers in the array. Some locations may have numbers, and at the same time some locations may not have numbers.
Now let's rearrange this array.
Rice seedlings every number in this array from beginning to end. When scanning to the number labeled I. First the number (denoted by m) is not I. Let's say, then scan the next number.
If not, compare it with the number of M numbers.
It is assumed to be equal to the first m number. A repeating number is found (the number appears in the position of the subscript I and M). Assuming that it and the number m do not want to wait, the first and second numbers are exchanged, and the M is placed in its place. Next, repeat the comparison. The process of exchange. Until a repeated number is found.
Use the array {2,3,1,0,2,5,3} as an example to analyze the steps to find repeated numbers.
The No. 0 digit of the array (counting from 0, and the subscripts of the arrays) is 2, and its subscript does not want to wait, so it is swapped with the number 1 labeled 2. The swapped array is {1,3,2,0,2,5,3}. At this point the No. 0 number is 1, still with its subscript do not want to wait, continue to put it and subscript 1 of the number 3 exchange, get the array {0. 1. 2. 3,2,5,3}. At this point the No. 0 digit is 0. Then scan the next number, in the next few numbers. The subscript is 1. 2. 3 of the three numbers are 1 respectively. 2, 3, their subscripts and values are equal, so there is no need to do whatever. Next scan the number labeled 4 to 2. Since its value is not labeled with its subscript, compare it with the number of subscript 2.
Notice that the number labeled 2 in the array is also 2, which is the number 2 and subscript 2 and subscript 4 of two positions, so a repeated number is found.
Java code implementations such as the following:
/** * Array of repeated numbers */package swordforoffer;/** * @author Jinshuangqi * * August 12, 2015 */public class E51duplicationinarray {Publ IC Boolean duplicate (int[] arr) {if (arr== null | | arr.length <= 0) {return false;} for (int i = 0;i<arr.length;i++) {if (Arr[i] < 0 | | arr[i] > ARR.LENGTH-1) return false;} for (int i = 0;i<arr.length;i++) {while (arr[i]! = i) {if (arr[i] = = Arr[arr[i]) {System.out.println (arr[i]); return true ;} Else{int temp = arr[i];arr[i] = arr[temp];arr[temp] = temp;}}} return false;} public static void Main (string[] args) {int[] arr = {2,3,1,0,2,5,3}; E51duplicationinarray test = new E51duplicationinarray (); System.out.println (Test.duplicate (arr));}}
. In the code, though, there are one or two iterations of the exchange. But each number can only be exchanged at most two times to find their own position, so the total time complexity is O (n), in addition, all of the operation steps are on the input array. No additional memory allocation is required. So the space complexity is O (1).
The sword refers to the offer surface question (Java edition): The repeated numbers in the array