【leetcode】Array—— Majority Element(169)

题目:

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.

思路1:sort  很简单,把nums排序后,直接取下彪为num.length/2的值

代码:

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

思路2:HashMap  维护一下HashMap<Integer,Integer>,存每个元素出现的次数,找到次数大于nums.length/2的key即可,该方法runtime 较长,不推荐

代码:

    public int majorityElement(int[] nums) {
        int len = nums.length;
        Map<Integer,Integer> map = new HashMap<Integer,Integer>();
        
        for(int i=0;i<nums.length;i++){
        	Integer count = map.get(nums[i]);
        	map.put(nums[i], (count==null?1:++count));
        	if(map.get(nums[i])>len/2)
        		return nums[i];
        }
    	
    	return -1;
    }

思路3:Moore voting 还是第一次接触这个算法,leetcode上面看到的。看看别人的对该算法的理解:http://blog.csdn.net/chfe007/article/details/42919017

代码:

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;
}

思路4:Bit Manipulation 十分巧妙的算法!!用int bit[32] 记录nums中的每个元素(int类型),每位(int是32位)的1出现的次数。

例如,bit[32]初始化为[0,0,…,0,0], nums=[1,1,1,2,2], 则bit[32]=[0,0,…,0,0,2,3] 

然后遍历bit中统计哪些位次数大于n/2,设为1,最后32位的bit就是结果的二进制

代码:

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;
}

思路5:Divide and Conquer

还是看看leetcode上面的解释吧,表示没看懂……

This idea is very algorithmic. However, the implementation of it requires some careful thought about the base cases of the recursion. The base case is that when the array has only one element, then it is the majority one. 

代码:

class Solution {
public:
    int majorityElement(vector<int>& nums) {
        return majority(nums, 0, nums.size() - 1);
    }
private:
    int majority(vector<int>& nums, int left, int right) {
        if (left == right) return nums[left];
        int mid = left + ((right - left) >> 1);
        int lm = majority(nums, left, mid);
        int rm = majority(nums, mid + 1, right);
        if (lm == rm) return lm;
        return count(nums.begin() + left, nums.begin() + right + 1, lm) > count(nums.begin() + left, nums.begin() + right + 1, rm) ? lm : rm;
    }
}; 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值