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.

也就是找数组中出现次数大于一半的数字,题目保证这个数字存在。

方法一:位操作

针对数组中每一个数的每一位,计算每一位上0和1出现的次数,取出现多的作为最终数字的当前位。

代码如下:时间复杂度32n=O(n),空间复杂度O(1)

// bit操作
    public int majorityElement(int[] nums) {
        int temp = 0, ans = 0, count0 = 0, count1 = 0;
        for (int i = 0; i < 32; i++) {
            count0 = 0;
            count1 = 0;
            for (int j = 0; j < nums.length; j++) {
                if (((nums[j] >>> i) & 1) == 1)
                    count1++;
                else
                    count0++;
            }
            if (count1 > count0)
                temp += 1;
            if (i < 31)
                temp >>>= 1;
        }
        for (int i = 0; i < 32; i++) {
            ans += ((temp >> i) & 1);
            if (i < 31)
                ans <<= 1;
        }
        return temp;
    }

方法二:HashMap,

空间复杂度O(n),时间复杂度O(n),相对位操作更耗时。

// 使用hashMap
    public int majorityElement(int[] nums) {
        int temp = 0, ans = 0, count0 = 0, count1 = 0;
        Map<Integer, Integer> countMap = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            countMap.put(nums[i], countMap.getOrDefault(nums[i], 0) + 1);
            if (countMap.get(nums[i]) > nums.length / 2)
                return nums[i];
        }
        return 0;
    }

方法三:计数法,

这个方法应该是最优解法吧。相比位操作更好。

该方法的思路是从局部思考问题,前2K个数字中,某个数无法做成majority element,但它也有可能出现很多次,但是最终在某个点上会被其他不相同的数字中和了。从后面再计数。最终找到的就是majority element。(描述的不好,直接看代码理解吧)。

假设被中和的是majority element,不用担心,因为你干掉了和你一样多的对手,在后续的子数组中,你还是大头。

假设被中和的不是,那么后续子数组中,你还是不能。

代码如下:

public int majorityElement(int[] nums) {
        int count = 0;
        int ans = nums[0];
        for (int i : nums) {
            if (count == 0)
                ans = nums[i];
            if (ans == nums[i])
                count++;
            else
                count--;
        }
        return ans;
    }
时间: 2025-01-07 09:34:04

LeetCode 169. Majority Element解题方法的相关文章

[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(求众数)

题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ? n/2 ? 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] 输出: 3 示例 2: 输入: [2,2,1,1,1,2,2] 输出: 2 思路 思路一: 利用哈希表的映射,储存数组中的数字以及它们出现的次数,当众数出现时,返回这个数字. 思路二: 因为众数是出现次数大于n/2的数字,所以排序之后中间的那个数字一定是众数.即nums[n/2]为众数.但是在计算比

【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. Credits: 自己的思路大概是建立一个map,

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

在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素. 我想到的方法是 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]

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(寻找发帖水王)原题,如果删

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 (众数)

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 忘记说了,特地回来补充,今天看完<