【leetcode 哈希表】Majority Element

【leetcode 哈希表】Majority Element 

@author:wepon

@blog:http://blog.csdn.net/u012162613


1、題目

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.

You may assume that the array is non-empty and the majority element always exist in the array.


2、分析

題意:給定一個長度爲n的數組,找出majority element,所謂majority element就是出現次數大於n/2的那個數。
很簡單的題目,解法很多:
  1. Runtime: O(n2) — Brute force solution: Check each element if it is the majority element.
  2. Runtime: O(n), Space: O(n) — Hash table: Maintain a hash table of the counts of each element, then find the most common one.
  3. Runtime: O(n log n) — Sorting: Find the longest contiguous identical element in the array after sorting.
  4. Average runtime: O(n), Worst case runtime: Infinity — Randomization: Randomly pick an element and check if it is the majority element. If it is not, do the random pick again until you find the majority element. As the probability to pick the majority element is greater than 1/2, the expected number of attempts is < 2.
  5. Runtime: O(n log n) — Divide and conquer: Divide the array into two halves, then find the majority element A in the first half and the majority element B in the second half. The global majority element must either be A or B. If A == B, then it automatically becomes the global majority element. If not, then both A and B are the candidates for the majority element, and it is suffice to check the count of occurrences for at most two candidates. The runtime complexity, T(n) = T(n/2) + 2n = O(n log n).
  6. Runtime: O(n) — Moore voting algorithm: We maintain a current candidate and a counter initialized to 0. As we iterate the array, we look at the current element x:
    1. If the counter is 0, we set the current candidate to x and the counter to 1.
    2. If the counter is not 0, we increment or decrement the counter based on whether x is the current candidate.
    After one pass, the current candidate is the majority element. Runtime complexity = O(n).
  7. Runtime: O(n) — Bit manipulation: We would need 32 iterations, each calculating the number of 1's for the ith bit of all n numbers. Since a majority must exist, therefore, either count of 1's > count of 0's or vice versa (but can never be equal). The majority number’s ith bit must be the one bit that has the greater count.
下面給出hash table的代碼,其他的有空再寫到評論裏吧。

3、代碼

 int majorityElement(vector<int> &num) {
        unordered_map<int,int> map;
        int len=num.size();
        for(int i=0;i<len;i++){
            if(map.find(num[i])==map.end()) map[num[i]]=0;
            else   map[num[i]]++;
        
            if(map[num[i]]>=len/2) return num[i];
        }
    }



發佈了72 篇原創文章 · 獲贊 438 · 訪問量 172萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章