Wins the offer - 41 times more than half of the array numbers appearing in

Title Description

There are a number of array number that appears more than half the length of the array, find this number. For example, a length of the input array 9 {1,2,3,2,2,2,5,4,2}. Since the number 2 appears five times, more than half the length of the array in the array, the output 2. If there is 0 output.
 
answer:
  Nothing to speak of
  
 1 class Solution {
 2 public:
 3     int MoreThanHalfNum_Solution(vector<int> numbers) {
 4         unordered_map<int, int>map;
 5         for (auto a : numbers)
 6         {
 7             map[a]++;
 8             if (map[a] > numbers.size() / 2)
 9                 return a;
10         }
11         return 0;
12     }
13 };

 

Guess you like

Origin www.cnblogs.com/zzw1024/p/11688947.html