Sword finger Offer 3. Duplicate number in array

(1) find the repeated numbers in the array

subject

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 have been repeated, or how many times each number has been repeated. Please find any duplicate number in the array. For example, if an array {2, 3, 1, 0, 2, 5, 3} with an input length of 7, the corresponding output is a repeating number 2 or 3.

Solution 1[Java]: dynamic reordering plus judgment

Put the element with value I in position i. if it is equal to the element value in position I, it can be judged that the element is duplicate.

public class Solution {
    public boolean duplicate(int[] numbers, int length, int[] duplication) {
        // check whether the array is valid
        if (numbers == null || length <= 0)
            return false;
        for (int i = 0; i < length; i++) {
            if (numbers[i] < 0 || numbers[i] > length - 1) {
                return false;
            }
        }

        for (int i = 0; i < length; i++) {
            if (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[] nums, int i, int j) {
        int t = nums[i];
        nums[i] = nums[j];
        nums[j] = t;
    }
}

Complexity analysis

The time complexity is O(n)O(n)O(n). Because the original array is operated directly, the space complexity is O(1)O(1)O(1).

Solution 2[Java]: sort and judge

Solution 3[Java]: use Hash data structure

summary

Solution 1 is only suitable for finding and repeating numerical problems. Solution 2 and solution 3 can also be used for searching repetition of objects.

(2) find out the repeated numbers without modifying the array

subject

All numbers in an array of length n+1 are in the range of 1 to n, so at least one number in the array is repeated. Please find any duplicate number in the array, but the input array cannot be modified. For example, if an array {2, 3, 5, 4, 3, 2, 6, 7} with an input length of 8, the corresponding output is a repeating number 2 or 3.

Solution 1[C + +]: number determination

#include <iostream>

int countRange(const int* numbers, int length, int start, int end);

// Parameters:
//        numbers: an array of integers
//        Length: the length of the array
// Return value:             
//        Positive number - the input is valid and there are duplicate numbers in the array. The return value is a duplicate number
//        Negative - invalid input or no duplicate number in array
int getDuplication(const int* numbers, int length)
{
    if(numbers == nullptr || length <= 0)
        return -1;

    int start = 1;
    int end = length - 1;
    while(end >= start)
    {
        int middle = ((end - start) >> 1) + start;
        int count = countRange(numbers, length, start, middle);
        if(end == start)
        {
            if(count > 1)
                return start;
            else
                break;
        }

        if(count > (middle - start + 1))
            end = middle;
        else
            start = middle + 1;
    }
    return -1;
}

int countRange(const int* numbers, int length, int start, int end)
{
    if(numbers == nullptr)
        return 0;

    int count = 0;
    for(int i = 0; i < length; i++)
        if(numbers[i] >= start && numbers[i] <= end)
            ++count;
    return count;
}

Related links

  1. (1) find out the duplicate number author source code in the array - Github
  2. (2) find out the duplicate digital author source code without modifying the array - Github
  3. (1) find out the repeated numbers in the array - niukewang

Tags: Java github

Posted on Fri, 08 Nov 2019 11:46:58 -0500 by pbsonawane