ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

majority 求众数系列题目

2021-09-25 23:34:54  阅读:136  来源: 互联网

标签:map 题目 nums int secondCount ++ majority num 众数


169. Majority ElementGiven an array nums of size n, return the majority element.The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.

Example 1:

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

Example 2:

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

Constraints:

  • n == nums.length
  • 1 <= n <= 5 * 104
  • -231 <= nums[i] <= 231 - 1
class Solution {
    public int majorityElement(int[] nums) {
        //排序
        Arrays.sort(nums);
        //排序后找中位数肯定是那个众数,因为他出现的次数超过了1/2
        return nums[nums.length/2];
    }
}

 

Could you solve the problem in linear time and in O(1) space?
class Solution {
    public int majorityElement(int[] nums) {
        int curr = 0;
        int count = 0;
        for(int num:nums){
            if(count==0) curr = num;
            if(curr==num) count++;
            else count--;
        }
        return curr;
    }
}

 

229. Majority Element II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times.

Follow-up: Could you solve the problem in linear time and in O(1) space?

Example 1:

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

Example 2:

Input: nums = [1]
Output: [1]

Example 3:

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

Constraints:

  • 1 <= nums.length <= 5 * 104
  • -109 <= nums[i] <= 109
class Solution {
    public List<Integer> majorityElement(int[] nums) {
        //该题最多存在两个众数,因为每个众数出现频率要超过1/3
        int first=0,firstCount = 0;
        int second=0,secondCount = 0;
        List<Integer> result  = new ArrayList();
        //第一次遍历进行摩尔投票,筛选出两个数字
        for(int num:nums){
            if(num==first) firstCount++;
            else if(num==second) secondCount++;
            //如果哪个count为0,用当前值替换
            else if(firstCount==0){
                first = num;
                firstCount++;
            }
            else if(secondCount==0){
                second = num;
                secondCount++;
            }
            //如果两个都不是,两个都--
            else{
                firstCount--;
                secondCount--;
            }
        }
        //第二次便利,校验是否大于1/3
        firstCount=0;
        secondCount=0;
        for(int num:nums){
            if(num==first) firstCount++;
            else if(num==second) secondCount++;
        }
        //将大于1/3的数字放入结果集
        if(firstCount>nums.length/3) result.add(first);
        if(secondCount>nums.length/3) result.add(second);
        return result;
    }
}
48 · Majority Number III 

Given an array of integers and a number k, the majority number is the number that occurs more than 1/k of the size of the array.

There is only one majority number in the array.

Example 1:

Input:

nums = [3,1,2,3,2,3,3,4,4,4]
k = 3

Output:

3

Explanation:

3 appeared four times, more than 10 / 3.
Example 2:

Input:

nums = [1,1,2]
k = 3

Output:

1

Explanation:

1 appeared two times, more than 3 / 3.

 

 

public class Solution {
    /**
     * @param nums: A list of integers
     * @param k: An integer
     * @return: The majority number
     */
    public int majorityNumber(List<Integer> nums, int k) {
        Map<Integer,Integer> map = new HashMap();
        for(int num:nums){
            if(map.containsKey(num)) map.put(num,map.get(num)+1);
            else{
                if(map.size()<k){
                    map.put(num,1);
                }
                else{
                    List<Integer> list  = new ArrayList();
                    for(int key:map.keySet()){
                        map.put(key,map.get(key)-1);
                        if(map.get(key)==0) list.add(key);
                    }
                    for(int ele:list){
                        map.remove(ele);
                    }
                }
            }
        }
        for(int key:map.keySet()) map.put(key,0);
        for(int num:nums){
            if(map.containsKey(num)) {
                int count = map.get(num);
                map.put(num,++count);
                if(count>nums.size()/k) return num;            
            }
        }
        return 0;   
    }
}

 

 

标签:map,题目,nums,int,secondCount,++,majority,num,众数
来源: https://www.cnblogs.com/cynrjy/p/15335579.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有