Sword refers to the repeated number in the array of offer

Insert picture description here
method one:

    public boolean duplicate(int numbers[],int length,int [] duplication) {
    
    
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0;i<length;i++) {
    
    
            if (list.contains(numbers[i])) {
    
    
                duplication[0] = numbers[i];
                return true;
            }else {
    
    
                list.add(numbers[i]);
            }
        }
        return false;

    }

Method 2:
Knowing that each number value in the array is less than the length of the array, if there are no repeated numbers in the array, the array should be sorted from small to large to satisfy that the element value corresponding to the i-th position is i. Use this feature (array {2, 3, 1, 0, 2, 5, 3}, pointer'|'):

|2 3 1 0 2 5 3
The position of the pointer'|' at this time is 0, and the value of the element at position 0 in the array is 2, 2 != 0, exchange the elements at position 0 and position 2 in the array.
|1 3 2 0 2 5 3
The position of the pointer'|' is still 0, and the value of the element at position 0 in the array is 1, 1 != 0, exchange the elements at position 0 and position 1 in the array.
|3 1 2 0 2 5 3
The position of the pointer'|' is still 0, and the value of the element at position 0 in the array is 3, 3 != 0, exchange the elements at position 0 and position 3 in the array.
|0 1 2 3 2 5 3
The position of the pointer'|' is still 0, but the value of the element at position 0 in the array is 0, 0 == 0, and the pointer'|' is moved one bit to the right.
0 |1 2 3 2 5 3
The position of the pointer'|' at this time is 1, but the value of the element at position 1 in the array is 1, and 1 == 1. Move the pointer'|' one bit to the right.
0 1 |2 3 2 5 3
The position of the pointer'|' at this time is 2, but the value of the element at position 2 in the array is 2, 2 == 2, and the pointer'|' is moved one bit to the right.
0 1 2 |3 2 5 3
The position of the pointer'|' at this time is 3, but the value of the element at position 3 in the array is 3, and 3 == 3. Move the pointer'|' one bit to the right.
0 1 2 3 |2 5 3
The position of the pointer'|' at this time is 4, and the value of the element at position 4 in the array is 2, 2 != 4, and the value of the element at position 4 and position 2 in the array is equal, then output The value of this element.

    public boolean duplicate2(int numbers[],int length,int [] duplication) {
    
    
             if (numbers == null||numbers.length == 0) {
    
    
                 return false;
             }
             for (int i = 0;i<length;i++) {
    
    
                 while (numbers[i] != i) {
    
    
                     if (numbers[i] == numbers[numbers[i]]) {
    
    
                         duplication[0] = numbers[i];
                         return true;
                     }
                     int tmp = numbers[i];
                     numbers[i] = numbers[tmp];
                     numbers[tmp] = tmp;
                 }
             }
             return false;

    }

Guess you like

Origin blog.csdn.net/AIJXB/article/details/113634558