[Offer] to prove safety of the algorithm in question duplicate digital array 03. (C ++)

 
All numbers in a length of the array in the nums n are 0 ~ N- . 1 range. Some digital array is duplicated, but do not know how many numbers repeat, do not know each number was repeated several times. Please find an array of any one of the duplicate numbers. 

[Example 1 ] 

Input: 
[ 2 , 3 , 1 , 0 , 2 , 5 , 3 ] 
Output: 2 or 3 

of the limitation] 

2 <= n-<= 100000 

[Problem-solving ideas]

A thought: first sort the array, and then traverse to find duplicate numbers.

Sorted using the sort function built in C ++, this method is time complexity O (nlogn), the spatial complexity is O (1).

class Solution {
public:
  int findRepeatNumber(vector<int>& nums) {
    sort(nums.begin(),nums.end());
    for(int i=0;i<nums.size()-1;i++){
      if(nums[i]==nums[i+1])
      return nums[i];
    }
    return -1;

  }
};

 

Thinking two: using the hash table. From the beginning through the array, if the element is not in the hash table, then add to the table a new key-value pair, if the element in the hash table, then returns that element. The method, time complexity is O (n), the spatial complexity is O (n).

class Solution {
public:
  int findRepeatNumber(vector<int>& nums) {
    unordered_map<int,int> bp;
    for(int i=0;i<nums.size();i++){
      if(bp.find(nums[i])==bp.end())
        bp.insert({nums[i],1});
      else
        return nums[i];
    }
    return -1;
  }

};

 

Thinking three: Is there a time complexity of O (n), space complexity is O (1) algorithm for it?

  We note that digital array are 0 to n-1 in. If the array does not duplicate numbers, then after the next array sort numbers i to i marked location appears. However, because the array has duplicate numbers, there may be some of the plurality of digital position, and some of these positions may not be digital. 
  Now let's rearrange the array, still scanning the array from start to finish once in each number. When the scanning to the subscript i of the digital first comparator figure (denoted by m) is not equal to i. If so, then scan the next number. If not, then let it compares the m numbers. 
  And if it is equal to the m-th digit, to find a repeatable number (this number is at index position i and m have appeared). If it is digital and m are not equal, put the i-th and m-th digit digital switching, into the position of the m belonging to it. Then reread this comparison, the exchange process, until we find a duplicate numbers. 
  An array {2,3,1,0,2,5,3} example to analyze digital find duplicate steps. An array of digits 0 (counting from zero, and the array index consistent) is 2, and an index to it are not equal, then it is the number 2 and the subscript 1 exchange. After switching array is 1.3.2.0.2.5.3 {}. At this time, the first digit is a 0, it is still not equal to the index, and it continues to lower the exchange 3 as number 1, to obtain the array {3,1,2,0,2,5,3}. Pick continue down the exchange of digital 0 and 3 0 3 digits, to obtain the array {0,1,2,3,2,5,3}. At this time, the digital value of 0 is 0, then scan the next number. Over the next several figures, the subscript numbers 1, 2, three were 1, 2, and their values are subscripts are equal, there is no need to do anything. Next to the scan 4 as number 2. Due to its value and its index are not equal, then compare it to digital and the subscript 2. Noting this point array subscript 2 is the number 2, i.e. the lower figures 2 and labeled at two positions labeled 4 have emerged, so finding a duplicate numbers.

class Solution {
public:
  int findRepeatNumber(vector<int>& nums) {
    for(int i=0; i < nums.size(); i++) {
      while(nums[i] != i) {
      if(nums[i] == nums[nums[i]])
        return nums[i];
      else
        swap(nums[i],nums[nums[i]]);
      }
    }
    return -1;
  }

};

 

Guess you like

Origin www.cnblogs.com/ziziQ/p/12506206.html