Algorithm Multiple Solutions - Repeated Numbers in JZ3 Array (Hash & STL)

topic


Solution 1 (only correct answers are satisfied)

time complexity

Double loop pairwise comparison - O(n^2)

ideas

  • The simplest method is to record the length of the array and scan directly in a double loop.
  • Starting from the first number, if the following number is the same, then this number is repeated and returns directly;
  • Otherwise, start from the second and third numbers... to determine whether the numbers appearing later are the same, and return directly if they are the same.
  • If the same number does not appear until the last scan, it means that there is no same number in this group of numbers.
  • j=i+1 avoids repetition and increases complexity when comparing.

code

class Solution {
public:
    int duplicate(vector<int>& numbers) {
        int len = numbers.size();
        for(int i=0; i<len; i++){
            for(int j=i+1; j<len; j++) {
                if(numbers[i]==numbers[j]) return numbers[i];
            }
        }
        return -1;
    }
};

Solution 2 (Advanced 1 & Map)

time complexity

O(n)

ideas

  • map lookup dictionary
  • Use map to define an array a (the initial value of the array elements is 0), and the value of the initial array corresponds to the element subscript k of the array a.
  • If the value appears repeatedly, it corresponds to the array element value a[k]++ of subscript k.
  • This will record the number of times all numbers appear.
  • Finally, scan the a array once to see if any element value is greater than 1 (that is, a[k]>1) to detect whether the initial array has repeated numbers, and if so, return numbers[i].

code

class Solution {
public:
    int duplicate(vector<int>& numbers) {
        map<int,int> a;
        for(int i=0; i<numbers.size(); i++){
            a[numbers[i]]++;
            if(a[numbers[i]]>1) return numbers[i];
        }
        return -1;
    }
};

Solution 3 (advanced 2 & unordered_map hash table)

time complexity

O(n)

ideas

Can find any repeating number or return all repeating numbers or all repeating numbers.

code

class Solution {
public:
    int duplicate(vector<int>& numbers) {
        unordered_map<int, int> numMap;
        for(auto &num:numbers){
            numMap[num]++;
        }
        for(auto it=numMap.begin(); it!=numMap.end(); it++){
            if(it->second>=2){
                return it->first;
            }
        }
        return -1;
    }
};

Solution 4 (Advanced 3 & Vector)

[C++] STL standard template library (Vector container - super detailed step-by-step example code explanation) - Programmer Sought

time complexity

O(n)

ideas

  • Requires time complexity O(n) - cannot be done using sorting.
  • Also requires a space complexity of O(n) - which can be done using the classical space-for-time method.
  • Create an array count of the same length as the input array numbers to record how many times each number occurs.
  • Traverse the array, each time a number is traversed, the element in the count is subscripted as ++, and the value of the element after ++ is judged.
  • If the value is greater than 1, the number is repeated, and the number is returned directly.
  • If traversing the entire array does not return, indicating that there are no duplicates, return -1.

code

class Solution {
public:
    int duplicate(vector<int>& numbers) {
        vector<int> count(numbers.size(),0);
        for(int i=0; i<numbers.size(); i++){
            if(++count.at(numbers.at(i))>1){
                return numbers.at(i);
            }
        }
        return -1;
    }
};

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325198036&siteId=291194637