The array of redundant array Java implementation [to prove safety offer]

topic

All numbers in a length of n in the array are in the range 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.

answer

1 achieved by sorting

1.1 Description

By way of invocation sequencing, is obtained from small to large arrays, comparing neighboring values ​​are equal, the elements can be obtained by repeating this case time complexity of sorting O (nlogn), the spatial complexity is O (1)

1.2code

 1 import java.util.*;
 2 public class Solution {
 3     public boolean duplicate(int numbers[],int length,int [] duplication) {
 4         if(numbers == null || length == 0){
 5             return false;
 6         }
 7         Arrays.sort(numbers);
 8         for(int i=0;i<length-1;i++){
 9             if(numbers[i] == numbers[i+1]){
10                 duplication[0] = numbers[i];
11                 return true;
12             }
13         }
14         return false;
15     }
16 }

 

2 is achieved by a hash table

2.1 Description

Scanning array, it is determined whether the current value of the hash table exists, if there is a repeated number, if there is added, the time complexity of O (n), the spatial complexity of O (n)

2.2code

 1 import java.util.*;
 2 public class Solution {
 3     public boolean duplicate(int numbers[],int length,int [] duplication) {
 4         Set<Integer> set = new HashSet<>();
 5         for(int i =0 ;i<length;i++){
 6             if(set.contains(numbers[i])){
 7                 duplication[0] = numbers[i];
 8                 return true;
 9             }else{
10                 set.add(numbers[i]);
11             }
12         }
13         return false;
14     }
15 }

 

3 is achieved by an array subscript

3.1 Description

Scanning array, determines the value of the subscript i is equal to i, if i is equal, it is determined whether the value of the subscript Numbers [i] equal numbers [numbers [i]], if equal to repeat, then the switch is not equal, time complexity of O (n), the spatial complexity is O (1)

3.2code

. 1  public  class Solution {
 2      public  Boolean Duplicate ( int Numbers [], int length, int [] Duplication) {
 . 3          IF (Numbers == null || length == 0 ) {
 . 4              return  to false ;
 . 5          }
 . 6          int NUM = 0 ; // subscript repetition number
 7          // iterate 
. 8          for ( int i = 0; i <numbers.length; i ++ ) {
 . 9              // determines whether i = numbers [i] 
10              the while(! I = Numbers [I]) { // is not equal, it is determined that m = numbers [i] is equal Numbers [m] 
. 11                  int m = [I] Numbers;
 12 is                  IF (m == Numbers [m]) { // If the target value is equal to the target value m i, is repeated 
13 is                      Duplication [0] = m;
 14                      return  to true ;
 15                  } the else { // If the target is not equal to the value m i is the target value, to be exchanged, the subscript i to position value of m, so that the Numbers [m] m == 
16                      Numbers [i] = Numbers [m];
 . 17                      Numbers [m] = m;
 18 is                  }
 . 19              }
 20 is          }
21          return false;
22     }
23 }

 

Guess you like

Origin www.cnblogs.com/ERFishing/p/11823161.html