leetcode[169] Majority Element

在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素。

我想到的方法是

1. 排序,然后扫描一次就知道了。总共nlgn

2. 哈希,记录每个次数,O(n)的时间和空间。

class Solution {
public:
    int majorityElement(vector<int> &num) {
        unordered_map<int, int> umap;
        for (int i = 0; i < num.size(); i++)
        {
            umap[num[i]]++;
        }
        for (unordered_map<int, int>::iterator itr = umap.begin(); itr != umap.end(); itr++)
        {
            if (itr -> second > num.size()/2)
                return itr -> first;
        }
    }
};
View Code

再介绍两个O(n)时间O(1)空间的方法:

1. 投票算法,因为符合的众数总是存在。所以没出现一对不一样的就可以忽视这一对。我们可以用计数表示,来一个数字,如果和candidate相同那么count++,否则count--,如果count为零了,那么candidate就是下一个数。最后candidate就是众数了。

class Solution {
public:
    int majorityElement(vector<int> &num) {
        int nTimes = 0;
        int candidate = 0;
        for(int i = 0; i < num.size(); i ++)
        {
            if(nTimes == 0)
            {
                candidate = num[i];
                nTimes = 1;
            }
            else
            {
                if(candidate == num[i])
                    nTimes ++;
                else
                    nTimes --;
            }
        }
        return candidate;
    }
};
View Code

2. 因为给定的是32位数,并且众数一定存在,那么每一位去考虑的话,对于每一位1或者0多的肯定是属于众数的。这样就知道众数了。

class Solution {
public:
    int majorityElement(vector<int> &num) {
        int bitCnt[32];
        memset(bitCnt, 0, sizeof(bitCnt));
        
        for (int i = 0; i < num.size(); i++)
        {
            for (int j = 0; j < 32; j++)
            {
                if (num[i] & (1 << j))
                    bitCnt[j]++;
            }
        }
        
        int ans = 0;
        for (int i = 0; i < 32; i++)
        {
            if (bitCnt[i] > num.size()/2)
                ans += (int)pow(2, i);
        }
        return ans;
    }
};
View Code

 

再附上官方的解法和网友的翻译:

    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 logn).
    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.

      时间复杂度: O(n2) — 蛮力法: 依次检查每一个元素是否为众数

      时间复杂度: O(n), 空间复杂度: O(n) — 哈希表: 维护一个每一个元素出现次数的哈希表, 然后找到出现次数最多的元素

      时间复杂度: O(n log n) — 排序: 在排序后找出连续重复出现次数最多的元素

      平均时间复杂度: O(n), 最坏复杂度: 无穷大 — 随机算法: 随机选取一个元素计算其是否为众数. 如果不是, 就重复上一步骤直到找到为止。 由于选出众数的概率 > 1 / 2, 因此期望的尝试次数 < 2

      时间复杂度: O(n log n) — 分治法: 将数组拆成2半, 然后找出前一半的众数A和后一半的众数B。则全局众数要么是A要么是B。 如果 A == B, 则它自然而然就是全局众数。 如果不是, 则A和B都是候选汇众数, 则至多只需要检查这两个元素的出现次数即可。 时间复杂度, T(n) = T(n/2) + 2n = O(n log n).

      时间复杂度: O(n) — Moore投票算法: 我们维护一个当前的候选众数和一个初始为0的计数器。遍历数组时,我们看当前的元素x:

      如果计数器是0, 我们将候选众数置为 x 并将计数器置为 1

      如果计数器非0, 我们根据x与当前的候选众数是否相等对计数器+1或者-1

      一趟之后, 当前的候选众数就是所求众数. 时间复杂度 = O(n).

      时间复杂度: O(n) — 位操作法: 我们需要32次迭代, 每一次计算所有n个数的第i位的1的个数。由于众数一定存在,那么或者1的个数 > 0的个数 或者反过来(但绝不会相同)。 众数的第i位一定是计数较多数字。

posted on 2014-12-23 23:42  higerzhang  阅读(1648)  评论(0编辑  收藏  举报