169. Majority Element

80 篇文章 0 订阅
24 篇文章 0 订阅

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.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

这道题的naive解法是O(n2),两个for循环,挨个统计数字出现的个数,超过n/2就返回,提交完之后第42个测试实例超时了,只能想复杂度更低的算法。

这里要隆重介绍一下O(n)复杂度的 Boyer–Moore majority vote algorithm。这是一个专门统计序列中出现频率最高的字符的算法,伪代码如下:

Initialize an element m and a counter i with i = 0
For each element x of the input sequence:
If i = 0, then assign m = x and i = 1
else if m = x, then assign i = i + 1
else assign i = i − 1
Return m

转换成这道题 java 的解法就是:

public class Solution {
    public int majorityElement(int[] nums) {
        int i = 0, count = 0, majority = 0;
        while (i < nums.length) {
            if (count == 0) {
                majority = nums[i];
                count ++;
            } else if (majority == nums[i]) {
                count ++;
            } else {
                count --;
            }
            i++;
        }
        return majority;
    }
}

实在是巧妙。其实这道题还有很多其他的解法,比如sorting, hashmap, moore voting, bit manipulation。具体的代码实现如下:

// Sorting
public int majorityElement1(int[] nums) {
    Arrays.sort(nums);
    return nums[nums.length/2];
}

// Hashtable 
public int majorityElement2(int[] nums) {
    Map<Integer, Integer> myMap = new HashMap<Integer, Integer>();
    //Hashtable<Integer, Integer> myMap = new Hashtable<Integer, Integer>();
    int ret=0;
    for (int num: nums) {
        if (!myMap.containsKey(num))
            myMap.put(num, 1);
        else
            myMap.put(num, myMap.get(num)+1);
        if (myMap.get(num)>nums.length/2) {
            ret = num;
            break;
        }
    }
    return ret;
}

// Moore voting algorithm
public int majorityElement3(int[] nums) {
    int count=0, ret = 0;
    for (int num: nums) {
        if (count==0)
            ret = num;
        if (num!=ret)
            count--;
        else
            count++;
    }
    return ret;
}

// Bit manipulation 
public int majorityElement(int[] nums) {
    int[] bit = new int[32];
    for (int num: nums)
        for (int i=0; i<32; i++) 
            if ((num>>(31-i) & 1) == 1)
                bit[i]++;
    int ret=0;
    for (int i=0; i<32; i++) {
        bit[i]=bit[i]>nums.length/2?1:0;
        ret += bit[i]*(1<<(31-i));
    }
    return ret;
}
最后一个 Bit manipulation 非常巧妙,因为序列中的数都是 int 型(32位)。分别统计序列中每一个数每一位出现的次数,大于n/2的位即属于出现次数大于n/2的那个数,把属于的位合并,得到最终结果,思想是Divide and Conquer。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值