算法练习5——多数元素

LeetCode 多数元素

给定一个大小为 n 的数组 nums ,返回其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。

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

蛮力法

  1. 双重循环,不过会超时
python 复制代码
class Solution:
    def majorityElement(self, nums: List[int]) -> int:
        times = 0
        for i in nums:
            for j in nums:
                if i == j:
                    times += 1
            if times > len(nums) / 2:
                return i
            times = 0
  1. 随机抽取,随机抽取一个元素为过半众数的概率大于1/2,该方法是基于数据特点对双重循环进行优化
python 复制代码
class Solution:
    def majorityElement(self, nums: List[int]) -> int:
        majority_count = len(nums) // 2
        while True:
            candidate = random.choice(nums)
            if sum(1 for elem in nums if elem == candidate) > majority_count:
                return candidate

# 作者:力扣官方题解
# 链接:https://leetcode.cn/problems/majority-element/
# 来源:力扣(LeetCode)
# 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  1. 哈希表计数
python 复制代码
class Solution:
    def majorityElement(self, nums: List[int]) -> int:
        counts = collections.Counter(nums)
        return max(counts.keys(), key=counts.get)

# 作者:力扣官方题解
# 链接:https://leetcode.cn/problems/majority-element/
# 来源:力扣(LeetCode)
# 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

数学方法,基于数据特征

  1. Boyer-Moore 投票算法
    过半众数是数组中出现次数最多的数据,如果每次从数组中抽取两个不同的数据,最后数组中剩下的一定是众数。
python 复制代码
class Solution:
    def majorityElement(self, nums: List[int]) -> int:
        item, times = nums[0], 1
        for idx in range(1, len(nums)):
            if nums[idx] != item:
                if times > 0:
                    times -= 1
                else:
                    item = nums[idx]
                    times = 1
            else:
                times += 1
        return item

还是官方写的简洁啊

python 复制代码
class Solution:
    def majorityElement(self, nums: List[int]) -> int:
        count = 0
        candidate = None

        for num in nums:
            if count == 0:
                candidate = num
            count += (1 if num == candidate else -1)

        return candidate

# 作者:力扣官方题解
# 链接:https://leetcode.cn/problems/majority-element/
# 来源:力扣(LeetCode)
# 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  1. 排序取中间值
    过半众数排序后,n/2位置处数据一定是过半众数
python 复制代码
class Solution:
    def majorityElement(self, nums: List[int]) -> int:
        nums.sort()
        return nums[len(nums) // 2]

# 作者:力扣官方题解
# 链接:https://leetcode.cn/problems/majority-element/
# 来源:力扣(LeetCode)
# 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
相关推荐
程序员东岸7 小时前
《数据结构——排序(中)》选择与交换的艺术:从直接选择到堆排序的性能跃迁
数据结构·笔记·算法·leetcode·排序算法
程序员-King.7 小时前
day104—对向双指针—接雨水(LeetCode-42)
算法·贪心算法
神仙别闹7 小时前
基于C++实现(控制台)应用递推法完成经典型算法的应用
开发语言·c++·算法
Ayanami_Reii7 小时前
进阶数据结构应用-一个简单的整数问题2(线段树解法)
数据结构·算法·线段树·延迟标记
listhi5208 小时前
基于改进SET的时频分析MATLAB实现
开发语言·算法·matlab
Keep_Trying_Go9 小时前
基于Zero-Shot的目标计数算法详解(Open-world Text-specified Object Counting)
人工智能·pytorch·python·算法·多模态·目标统计
xl.liu9 小时前
零售行业仓库商品数据标记
算法·零售
confiself9 小时前
通义灵码分析ms-swift框架中CHORD算法实现
开发语言·算法·swift
做怪小疯子9 小时前
LeetCode 热题 100——二叉树——二叉树的层序遍历&将有序数组转换为二叉搜索树
算法·leetcode·职场和发展
CoderYanger10 小时前
递归、搜索与回溯-记忆化搜索:38.最长递增子序列
java·算法·leetcode·1024程序员节