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.

给定一个数组,求其中权制最大的元素,(该元素出现超过了一半次数)。

直观想法,HashMap,很慢

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

discuss上面看到了这道题非常完善的总结:

1、排序sort

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

2、HashMap

3、Moore’s voting algorithm(最佳算法)

很巧妙,时间O(n)  ,空间O(1),就是记录当前元素以及当前元素的“胜出”数量(比其他元素多几个)。

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 

通过记录每一位上的数字来得出的结果,相比之下,还是第三种算法更好。

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;
}
时间: 2024-10-18 23:46:21

leetcode 169. Majority Element 求出现次数最多的数 --------- java的相关文章

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]为众数.但是在计算比

[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——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. Hide Tags: Divide and Conquer

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

leetcode 169. Majority Element 多数投票算法(Boyer-Moore Majority Vote algorithm)

题目: 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. 题解:运用多数投票算法的思路来解:从头到尾遍历数

Java for 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. 解题思路: 编程之美P130(寻找发帖水王)原题,如果删