[Sword Finger Offer Second Edition] [Interview Question 3: Repeated Numbers in Array]

Title: All numbers in an array of length n are in the range of 0 to n-1. Some numbers in the array are repeated, but I do not know how many numbers are repeated, nor how many times each number is repeated. Please find any duplicate number in the array.


Illustrative
  example, if the length of the input array 7 {2,3,1,0,2,5,3}, then the corresponding output is repeatable number 2 or 3.
  Problem Analysis
A simple method to solve this problem is to sort the input array first. It's easy to find duplicate numbers in the sorted array, just scan the sorted array from beginning to end. Sorting an array of length n takes O (nlogn) time.
  You can also use a hash table to solve this problem. Scan each number of the array in order from beginning to end. When scanning a number, you can use O (1) time to determine whether the number is already included in the hash table. If the number does not already exist in the hash table, add it to the hash table. If the number already exists in the hash table, then a duplicate number is found. The time complexity of this algorithm is O (n), but it improves time efficiency at the cost of a hash table of size O (n). Let us see if there is any algorithm with space complexity O (1).
  We noticed that the numbers in the array are all from 0 to n-1. If there are no duplicate numbers in this array, then the number i will appear at the position of subscript i after the array is sorted. Because there are repeated numbers in the array, there may be multiple numbers in some positions, and there may be no numbers in some positions.
Now let's rearrange this array, still scanning every number in this array from beginning to end. When scanning to a number with subscript i, first compare whether this number (indicated by m) is equal to i. If yes, then scan the next number. If not, compare it with the m-th number.
  If it is equal to the m-th number, a duplicate number is found (the number appears at both the subscripts i and m). If it is not equal to the m-th number, exchange the i-th number with the m-th number, and put m in its place. Next, re-read the comparison and exchange process until we find a duplicate number.
  Take the array {2,3,1,0,2,5,3} as an example to analyze the steps to find duplicate numbers. The 0th number of the array (counting from 0, consistent with the array subscript) is 2, which is not equal to its subscript, so it is exchanged with the number 1 with subscript 2. The array after the exchange is {1.3.2.0.2.5.3}. At this time, the 0th number is 1, which is still not equal to its subscript. Continue to exchange it with the number 3 with subscript 1, to get the array {3,1,2,0,2,5,3}. Next, continue to exchange the 0th digit 3 and the 3rd digit 0 to get the array {0,1,2,3,2,5,3}. At this time, the value of the 0th digit is 0, and then the next digit is scanned. In the next few numbers, the three numbers with subscripts 1, 2 and 3 are 1, 2, 3, respectively, and their subscripts and values ​​are equal, so no action is required. Next, scan to the number with subscript 4. Since its value is not equal to its subscript, compare it with the number with subscript 2. Note that the number with subscript 2 in the array at this time is also 2, that is, the number appears in two positions with subscript 2 and subscript 4, so a duplicate number is found.

 1 public class Test3 {
 2     public static int duplicate(int[] numbers){
 3         if (numbers == null || numbers.length<1){
 4             return -1;
 5         }
 6 
 7         for (int i:numbers){
 8             if (i<0 || i>=numbers.length){
 9                 return -1;
10             }
11         }
12 
13         for (inti = 0; i <numbers.length; i ++ ) {
 14              // Always exchange when number [i] is not the same as i 
15              while (numbers [i]! = i) {
 16                  // If the number and the same number [i] location, description duplicated digital 
. 17                  IF (numbers [I] == numbers [numbers [I]]) {
 18 is                      return numbers [I];
 . 19                  } the else {
 20 is                      the swap (numbers, I, numbers [ i]);
 21                  }
 22              }
 23          }
 24          return -1 ;
 25      }
 26  
27      private static void swap(int[] data, int x, int y) {
28         int tmp=data[x];
29         data[x]=data[y];
30         data[y]=tmp;
31     }
32 
33     public static void main(String[] args) {
34         int[] numbers={2,3,1,0,2,5,3};
35         System.out.println(duplicate(numbers));
36     }
37 }

 operation result

 

Reference blog: https://blog.csdn.net/derrantcm/article/details/46811855

 

Guess you like

Origin www.cnblogs.com/lkylin/p/12750487.html