LeetCode Majority Element I && II

54 篇文章 0 订阅
20 篇文章 0 订阅

原题链接在这里:Majority Element IMajority Element II

对于Majority Element I 来说,有多重解法。

Method 1:最容易想到的就是用HashMap 计数,数值大于n/2(注意不是大于等于而是大于),就是返回值。Time O(n), Space O(n).

Method 2: 用了sort,返回sort后array的中值即可。Time O(n*logn), Space O(1).

Method 3: 维护个最常出现值,遇到相同count++,遇到不同count--,count为0时直接更改最常出现值为nums[i].

AC Java:

public class Solution {
    public int majorityElement(int[] nums) {
        /*
        //Method 1, HashMap
        HashMap<Integer, Integer> map = new HashMap<>();
        for(int i = 0;i<nums.length; i++){
            if(!map.containsKey(nums[i])){
                map.put(nums[i],1);
            }else{
                map.put(nums[i],map.get(nums[i])+1);
            }
        }
        
        Iterator<Integer> it = map.keySet().iterator(); //Iterate HashMap
        while(it.hasNext()){
            int keyVal = it.next();
            //There is an error here: Pay attentation, it is ">", but not ">="
            //If we have three variables [3,2,3], ">=" will also return 2, 1>=3/2
            if(map.get(keyVal) > nums.length/2){
                return keyVal;
            }
        }
        
        return Integer.MIN_VALUE;
        */
        
        /*Method 2, shortcut
        Arrays.sort(nums);
        return nums[nums.length/2];
        */
        
        //Method 3
        if(nums == null || nums.length == 0)
            return Integer.MAX_VALUE;
        int res = nums[0];
        int count = 1;
        for(int i = 1; i< nums.length; i++){
            if(res == nums[i]){
                count++;
            }else if(count == 0){
                res = nums[i];
                count = 1;
            }else{
                count--;
            }
        }
        return res;
        
    }
}

Majority Element II 与  Majority Element I  的第三种方法很像,因为个数超过n/3的值最多有两个,现在改成维护两个最常出现的值。

最后再扫一遍数组,查看这两个最常出现值的实际出现次数,看看是否大于n/3,大于的都加入返回list中。

AC Java:

public class Solution {
    public List<Integer> majorityElement(int[] nums) {
        List<Integer> res = new ArrayList<Integer>();
        if(nums == null || nums.length == 0){
            return res;
        }
        
        int major1 = 0, major2 = 0;
        int count1 = 0, count2 = 0;
        for(int i = 0; i < nums.length; i++){
            if(major1 == nums[i]){
                count1++;
            }else if(major2 == nums[i]){
                count2++;
            }else if(count1 == 0){
                major1 = nums[i];
                count1 = 1;
            }else if(count2 == 0){
                major2 = nums[i];
                count2 = 1;
            }else{
                count1--;   //error
                count2--;
            }
        }
        count1 = 0;
        count2 = 0;
        for(int i = 0; i < nums.length; i++){
            if(nums[i] == major1){
                count1++;
            }else if(nums[i] == major2){
                count2++;
            }else{
                continue;
            }
        }
        if(count1 > nums.length/3){
            res.add(major1);
        }
        if(count2 > nums.length/3){
            res.add(major2);
        }
        return res;
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值