[LeetCode] 169. Majority Element 求大多数

 

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

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

Example 1:github

Input: [3,2,3]
Output: 3

Example 2:算法

Input: [2,2,1,1,1,2,2]
Output: 2

 

这是到求大多数的问题,有不少种解法,其中我感受比较好的有两种,一种是用哈希表,这种方法须要 O(n) 的时间和空间,另外一种是用一种叫摩尔投票法 Moore Voting,须要 O(n) 的时间和 O(1) 的空间,比前一种方法更好。这种投票法先将第一个数字假设为过半数,而后把计数器设为1,比较下一个数和此数是否相等,若相等则计数器加一,反之减一。而后看此时计数器的值,若为零,则将下一个值设为候选过半数。以此类推直到遍历完整个数组,当前候选过半数即为该数组的过半数。不仔细弄懂摩尔投票法的精髓的话,过一阵子仍是会忘记的,首先要明确的是这个叼炸天的方法是有前提的,就是数组中必定要有过半数的存在才能使用,下面来看本算法的思路,这是一种先假设候选者,而后再进行验证的算法。现将数组中的第一个数假设为过半数,而后进行统计其出现的次数,若是遇到一样的数,则计数器自增1,不然计数器自减1,若是计数器减到了0,则更换下一个数字为候选者。这是一个很巧妙的设定,也是本算法的精髓所在,为啥遇到不一样的要计数器减1呢,为啥减到0了又要更换候选者呢?首先是有那个强大的前提存在,必定会有一个出现超过半数的数字存在,那么若是计数器减到0了话,说明目前不是候选者数字的个数已经跟候选者的出现个数相同了,那么这个候选者已经很 weak,不必定能出现超过半数,此时选择更换当前的候选者。那有可能你会有疑问,那万一后面又大量的出现了以前的候选者怎么办,不须要担忧,若是以前的候选者在后面大量出现的话,其又会从新变为候选者,直到最终验证成为正确的过半数,佩服算法的提出者啊,代码以下:数组

 

C++ 解法一:app

class Solution {
public:
    int majorityElement(vector<int>& nums) {
        int res = 0, cnt = 0;
        for (int num : nums) {
            if (cnt == 0) {res = num; ++cnt;}
            else (num == res) ? ++cnt : --cnt;
        }
        return res;
    }
};

 

Java 解法一:post

public class Solution {
    public int majorityElement(int[] nums) {
        int res = 0, cnt = 0;
        for (int num : nums) {
            if (cnt == 0) {res = num; ++cnt;}
            else if (num == res) ++cnt;
            else --cnt;
        }
        return res;
    }
}

 

下面这种解法利用到了位操做 Bit Manipulation 来解,将这个大多数按位来创建,从0到31位,每次统计下数组中该位上0和1的个数,若是1多,那么将结果 res 中该位变为1,最后累加出来的 res 就是过半数了,至关赞的方法,参见代码以下:url

 

C++ 解法二:spa

class Solution {
public:
    int majorityElement(vector<int>& nums) {
        int res = 0, n = nums.size();
        for (int i = 0; i < 32; ++i) {
            int ones = 0, zeros = 0;
            for (int num : nums) {
                if (ones > n / 2 || zeros > n / 2) break;
                if ((num & (1 << i)) != 0) ++ones;
                else ++zeros;
            }
            if (ones > zeros) res |= (1 << i);
        }
        return res;
    }
};

 

Java 解法二:code

public class Solution {
    public int majorityElement(int[] nums) {
        int res = 0, n = nums.length;
        for (int i = 0; i < 32; ++i) {
            int ones = 0, zeros = 0;
            for (int num : nums) {
                if (ones > n / 2 || zeros > n / 2) break;
                if ((num & (1 << i)) != 0) ++ones;
                else ++zeros;
            }
            if (ones > zeros) res |= (1 << i);
        }
        return res;
    }
}

 

Github 同步地址:

https://github.com/grandyang/leetcode/issues/169

 

相似题目:

Majority Element II

 

参考资料:

https://leetcode.com/problems/majority-element/

https://leetcode.com/problems/majority-element/discuss/51613/O(n)-time-O(1)-space-fastest-solution

https://leetcode.com/problems/majority-element/discuss/51612/6-Suggested-Solutions-in-C++-with-Explanations

https://leetcode.com/problems/majority-element/discuss/51611/Java-solutions-(sorting-hashmap-moore-voting-bit-manipulation).

https://leetcode.com/problems/majority-element/discuss/51828/C++-solution-using-Moore's-voting-algorithm-O(n)-runtime-comlexity-an-no-extra-array-or-hash-table

 

LeetCode All in One 题目讲解汇总(持续更新中...)

相关文章
相关标签/搜索