Sword refers to offer (C++)-JZ3: repeated numbers in the array (algorithm - sorting)

Author: Zhai Tianbao Steven
Copyright statement: The copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source

Title description:

All numbers in an array of length n are in the range 0 to n-1. Some numbers in the array are repeated, but I don't know how many numbers are repeated. It is also not known how many times each number is repeated. Please find any repeated numbers in the array. For example, if the input is an array [2,3,1,0,2,5,3] of length 7, then the corresponding output is 2 or 3. If there is an illegal input, output -1

Data range: 0≤n≤10000 

Advanced: time complexity O(n), space complexity O(n) 

Example:

enter:

[2,3,1,0,2,5,3]

return value:

2

illustrate:

2 or 3 are both correct   

Problem-solving ideas:

This question is a sorting question, and there are four ways to solve it:

1) Law of Violence

       Double loop traversal, find duplicate numbers and return. Time complexity O(n^2), space complexity O(1).

2) Sort traversal method

       Sort the array with quick sort, and then traverse the search. Time complexity O(nlogn), space complexity O(1).

3) Hash table

       Use a hash table to determine whether a number is repeated, you can use a hash set or a hash map. Hash table lookup is O(1), so it only needs to be traversed in one round to complete. Time complexity O(n), space complexity O(n).

4) Subscript hashing

       Use the array subscript as the hash key, such as the number 2, put it at the position of the array subscript 2, and move the number at the original subscript 2 position to the original position of the number 2 to complete the exchange; and so on until a certain exchange finds The number x and the number of the subscript x conflict, indicating that a duplicate value has been found; the worst case is that there is no duplicate number, and all subscripts have been traversed and exchanged for one round. Time complexity O(n), space complexity O(1).

       Note: The premise of applying this method is that, as the conditions given in the title, the range of numbers is between 0 and n-1, otherwise it is out of bounds.

Test code:

1) Law of Violence

class Solution {
public:
    // 寻找重复数据
    int duplicate(vector<int>& numbers) {
        int size= int(numbers.size());
        for (int i = 0; i < size; ++i){
            for (int j = i + 1; j < size; ++j){
                if (numbers[i] == numbers[j])
                    return numbers[i];
            }
        }
        return -1;
    }
};

2) Sort traversal method

class Solution {
public:
    // 寻找重复数据
    int duplicate(vector<int>& numbers) {
        // 快排
        sort(numbers.begin(),numbers.end());
        int size = int(numbers.size());
        // 寻找重复值
        for(int i = 1; i <size; ++i){
            if(numbers[i] == numbers[i - 1])
                return numbers[i];
        }
        return -1;
    }
};

3) Hash table

class Solution {
public:
    // 寻找重复数据
    int duplicate(vector<int>& numbers) {
        // 哈希set
        unordered_set<int> us;
        int size = int(numbers.size());
        for(int i = 0; i < size; ++i){
            if(us.count(numbers[i]))
                return numbers[i];
            else
                us.insert(numbers[i]);
        }
        return -1;
    }
};

4) Subscript hashing

class Solution {
public:
    // 寻找重复数据
    int duplicate(vector<int>& numbers) {
        // 下标哈希法
        int size = int(numbers.size());
        for(int i = 0; i < size;){
            if(numbers[i]==i)
                i++;
            else{
                if(numbers[numbers[i]] == numbers[i])
                    return numbers[i];
                else{
                    swap(numbers[numbers[i]], numbers[i]);
                }
            }
        }
        return -1;
    }
};

Guess you like

Origin blog.csdn.net/zhaitianbao/article/details/131239966