Sword refers to offer (1): find the repeated numbers in the array

❝
涓滴之水终可以磨损大石,不是由于它力量强大,而是由于昼夜不舍的滴坠。——贝多芬

❞

Find repeated numbers in the array


Title description

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 don’t know how many numbers are repeated, nor do I know how many times each number is repeated. Please find any duplicate number in the array. For example, if the input length is 7 array {2, 3, 1, 0, 2, 5, 3}, then the corresponding output is the repeated number 2 or 3.

solution

Solution 1: After
sorting, scan sequentially to determine whether there is a repetition. The time complexity is O(n²).

Solution 2:
Use a hash table to traverse the array. If there is no such element in the hash table, store it in the hash table, otherwise return a duplicate element. The time complexity is O(n), and the space complexity is O(n).

Solution 3 The
length is n, and the value range of the elements is also n. If there are no repeated elements, then the value corresponding to each subscript of the array is equal to the subscript.

Traverse the array from beginning to end, when scanning to the number nums[i] of the subscript i:

  • If it is equal to i, continue to scan down;
  • If it is not equal to i, compare it with the nums[i]th number. If it is equal, it means there is a duplicate value and return nums[i]. If they are not equal, exchange the i-th number with nums[i]-th number. Repeat this comparative exchange process.
    The time complexity of this algorithm is O(n), because each element only needs to be exchanged twice at most to determine the position. The space complexity is O(1).
/**
 * @author bingo
 * @since 2018/10/27
 */

public class Solution {
    /**
     * 查找数组中的重复元素
     * @param numbers 数组
     * @param length 数组长度
     * @param duplication duplication[0]存储重复元素
     * @return boolean
     */
    public boolean duplicate(int[] numbers, int length, int[] duplication) {
        if (numbers == null || length < 1) {
            return false;
        }
        for (int e : numbers) {
            if (e >= length) {
                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;
                }
                swap(numbers, i, numbers[i]);
            }
        }

        return false;
    }

    private void swap(int[] numbers, int i, int j) {
        int t = numbers[i];
        numbers[i] = numbers[j];
        numbers[j] = t;
    }
}

Test case

  1. An array of length n contains one or more repeated numbers;
  2. The array does not contain repeated numbers;
  3. Invalid test input case (input a null pointer; the array of length n contains numbers other than 0~n-).

I organized all the problem solutions I wrote into an e-book and placed it on github, and hit the top of the github ranking in three days! Nearly 5w people downloaded and read! If you want to get it, just go to the link below (remember to give me a star):

https://github.com/geekxh/hello-algorithm

Sword refers to offer (1): find the repeated numbers in the array

Guess you like

Origin blog.51cto.com/15076236/2609662