Leetcode#169. Majority Element(求众数)

题目描述

给定一个大小为 n 的数组,找到其中的众数。众数是指在数组中出现次数大于 ? n/2 ? 的元素。

你可以假设数组是非空的,并且给定的数组总是存在众数。

示例 1:

输入: [3,2,3]
输出: 3

示例 2:

输入: [2,2,1,1,1,2,2]
输出: 2

思路

思路一:

利用哈希表的映射,储存数组中的数字以及它们出现的次数,当众数出现时,返回这个数字。

思路二:

因为众数是出现次数大于n/2的数字,所以排序之后中间的那个数字一定是众数。即nums[n/2]为众数。但是在计算比较大的数组时,时间会超过限制。

思路三:

分治法,将整个数组分成两个部分,先分别筛选出这两部分中出现次数最多的数,记为left和right,如果left等于right,则返回left,如果left不等于right,则left和right都是最终结果的候选,此时需要遍历整个数组考察left和right出现的次数,出现次数较多的就是最终返回的结果。

思路四:

摩尔投票算法,先将第一个数字假设为众数,然后把计数器设为1,比较下一个数和此数是否相等,若相等则计数器加一,反之减一。然后看此时计数器的值,若为零,则将当前值设为候选众数。以此类推直到遍历完整个数组,当前候选众数即为该数组的众数。

代码实现

package Array;

import java.util.HashMap;

/**
 * 169. Majority Element(求众数)
 * 给定一个大小为 n 的数组,找到其中的众数。众数是指在数组中出现次数大于 ? n/2 ? 的元素。
 * 你可以假设数组是非空的,并且给定的数组总是存在众数。
 */
public class Solution169 {
    public static void main(String[] args) {
        Solution169 solution169 = new Solution169();
        int[] nums = new int[]{3, 2, 3};
        System.out.println(solution169.majorityElement_4(nums));

    }

    /**
     * 利用哈希表的映射,储存数组中的数字以及它们出现的次数,当众数出现时,返回这个数字。
     *
     * @param nums
     * @return
     */
    public int majorityElement(int[] nums) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int num :
                nums) {
            Integer cnt = map.get(num);
            if (cnt == null) {
                cnt = 1;
            } else {
                cnt++;
            }
            if (cnt > nums.length / 2) {
                return num;
            }
            map.put(num, cnt);
        }
        return 0;
    }

    /**
     * 因为众数是出现次数大于n/2的数字,所以排序之后中间的那个数字一定是众数。即nums[n/2]为众数。但是在计算比较大的数组时,时间会超过限制。
     *
     * @param nums
     * @return
     */
    public int majorityElement_2(int[] nums) {
        int n = nums.length;
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - 1 - i; j++) {
                if (nums[j] > nums[j + 1]) {
                    int temp = nums[j];
                    nums[j] = nums[j + 1];
                    nums[j + 1] = temp;
                }
            }
        }
        return nums[n / 2];
    }

    /**
     * 分治法,将整个数组分成两个部分,先分别筛选出这两部分中出现次数最多的数,记为left和right,如果left等于right,则返回left
     * 如果left不等于right,则left和right都是最终结果的候选,此时需要遍历整个数组考察left和right出现的次数,出现次数较多的就是最终返回的结果。
     *
     * @param nums
     * @return
     */
    public int majorityElement_3(int[] nums) {
        return find(nums, 0, nums.length - 1);
    }

    public int find(int[] nums, int begin, int end) {
        if (begin == end) {
            return nums[begin];
        } else {
            int mid = (begin + end) / 2;
            int left = find(nums, begin, mid);
            int right = find(nums, mid + 1, end);
            //左右两部分的众数相同 则这个数是这部分的众数
            if (left == right) {
                return left;
            } else {
                //左右两部分的众数不相同 则这两个数都有可能是这部分的众数,那么遍历这个数组,看一下哪个数字的出现次数多
                int countLeft = 0;
                int countRight = 0;
                for (int i = begin; i <= end; i++) {
                    if (nums[i] == left) {
                        countLeft++;
                    } else if (nums[i] == right) {
                        countRight++;
                    }
                }
                if (countLeft > countRight) {
                    return left;
                } else {
                    return right;
                }
            }
        }
    }

    /**
     * 摩尔投票算法,先将第一个数字假设为众数,然后把计数器设为1,比较下一个数和此数是否相等,若相等则计数器加一,反之减一。
     * 然后看此时计数器的值,若为零,则将当前值设为候选众数。以此类推直到遍历完整个数组,当前候选众数即为该数组的众数。
     *
     * @param nums
     * @return
     */
    public int majorityElement_4(int[] nums) {
        int maj = nums[0];
        int count = 1;
        for (int num : nums) {
            if (maj == num) {
                count++;
            } else {
                count--;
                if (count == 0) {
                    maj = num;
                    count = 1;
                }
            }
        }
        return maj;
    }

}

原文地址:https://www.cnblogs.com/wupeixuan/p/9570770.html

时间: 2024-08-24 18:12:24

Leetcode#169. Majority Element(求众数)的相关文章

LeetCode 169. 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. 题目标签:Array 忘记说了,特地回来补充,今天看完<

leetcode 169. Majority Element 求出现次数最多的数 --------- java

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. 给定一个数组,求其中权制最大的元素,(该元素出现超过了一

169. Majority Element求众数

网址:https://leetcode.com/problems/majority-element/ 参考:https://blog.csdn.net/u014248127/article/details/79230221 可以直接通过map解决 利用神奇的 摩尔投票算法( Boyer-Moore Voting Algorithm) 不断的消去两个不同的数,最后剩下的数肯定是所求的众数! 细节: 若第一个数的出现次数不止 1,则“消去”意味着次数-1 如果两数相同,要将此数的次数累加 class

[Lintcode]46. Majority Element/[Leetcode]169. Majority Element

46. Majority Element/[169. Majority Element(https://leetcode.com/problems/majority-element/) 本题难度: Easy Topic: Greedy Description Given an array of integers, the majority number is the number that occurs more than half of the size of the array. Find

leetCode 169. Majority Element 数组

169. 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. 思路1: 使用m

[LeetCode] 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. Credits:Special thanks to @t

leetcode[169] Majority Element

在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素. 我想到的方法是 1. 排序,然后扫描一次就知道了.总共nlgn 2. 哈希,记录每个次数,O(n)的时间和空间. class Solution { public: int majorityElement(vector<int> &num) { unordered_map<int, int> umap; for (int i = 0; i < num.size(); i++) { umap[num[i]

LeetCode 169 Majority Element(主要元素)(vector、map)

翻译 给定一个长度为n的数组,找出主要的元素. 所谓主要的元素是指的出现次数超过? n/2 ?次的元素. 你可以假定这个数组是非空的,并且"主要元素"一定是存在的. 原文 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

leetcode 169 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. 思路: Find k different element