leetcode 169. Majority Element

33 篇文章 0 订阅
30 篇文章 0 订阅
  1. 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.

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

Example 1:

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

Example 2:

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

method 1

sort, 然后直接取中间,代码略

method 2

hashMap, 统计每个数字出现的次数,然后取最大

method 3

分治法,以中间为线,分别找到左边和右边的主元素,如果两个数相同,说明此就为主元素.如果不同,那分别统计他们出现的次数,次数多的为主元素

/**
 * 采用分治法,分别得到数组左边的主元素和右边的主元素,如果两边主元素是同一个数,那直接return
 * 如果不一样,就需要计算搜索范围看哪一个出现的次数更多就返回哪一个
 * @param nums
 * @return
 */
public int majorityElement2(int[] nums) {
    return majorityElementRec(nums, 0, nums.length - 1);
}
private int countInRange(int[] nums, int num, int lo, int hi) {
        int count = 0;
        for (int i = lo; i <= hi; i++) {
            if (nums[i] == num) {
                count++;
            }
        }
        return count;
    }

    private int majorityElementRec(int[] nums, int lo, int hi) {
        // base case; the only element in an array of size 1 is the majority
        // element.
        if (lo == hi) {
            return nums[lo];
        }

        // recurse on left and right halves of this slice.
        int mid = (hi-lo)/2 + lo;
        int left = majorityElementRec(nums, lo, mid);
        int right = majorityElementRec(nums, mid+1, hi);

        // if the two halves agree on the majority element, return it.
        if (left == right) {
            return left;
        }

        // otherwise, count each element and return the "winner".
        int leftCount = countInRange(nums, left, lo, hi);
        int rightCount = countInRange(nums, right, lo, hi);

        return leftCount > rightCount ? left : right;
    }
method 4

Boyer-Moore Voting Algorithm 多数投票算法

一旦两个相邻的元素不同,就把这两个元素对冲抵消掉。由于众数的出现频次大于数据其他元素出现频率次之和,所以这种抵消最后剩下的一定是众数

public int majorityElement(int[] nums){
    int count = 1;
    int res = nums[0];
    for(int i = 1; i< nums.length; i++){
        if(count == 0)
            res = nums[i];
        
       if(res == nums[i])
           count++;
        else{
            count--;
        }
    }
    
    return res;
}
method 5

位操作法:该法是我从一篇博客中学到了(忘记了原博的地址了)

因为一个整数在32为机器上只有32位,那么可以使用一个长度为32的数组来记录输入数组中每1位中1的个数和0的个数,由于存在一个出现次数超过一半的元素,那么取第i位中出现次数多的(0或者1)即可以构成超过数组一半元素。

public int majorityElement5(int[] nums) {
    int ones = 0;
    int zeros = 0;

    int res = 0;
    for (int i = 0; i < 32; i++) {
        for (int j = 0; j < nums.length; j++) {
            int num = nums[i];
            if ((num & (1 << i)) != 0)
                ones++;
            else zeros++;
        }

        if (ones > zeros)
            res |= (1 << i);
    }
    
    return res;
}
summary
  1. 找数量上的问题时,如果数量上是占一定优势,可以考虑多数投票算法
  2. 分治算法,分的每一部分都是对题目直接求答案,如本题是对各自部分求主元素,而不是求什么其他辅助条件,将大问题分为许多小问题
  3. 位操作,数字数量上的问题,最后会反应到32位上的每一位,可以借鉴这种解法
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值