算法练习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)
# 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
相关推荐
数模竞赛Paid answer8 小时前
2026年中青杯数学建模A题数学建模论文智能评估系统与多智能体优化方法求解全过程论文及程序
算法·数学建模·中青杯
银-豆豆8 小时前
数据结构与算法-动态规划、回溯与贪心
算法·动态规划
想吃火锅100510 小时前
【leetcode】200. 岛屿数量
算法·leetcode·职场和发展
Nil20811 小时前
leetcode 54螺旋矩阵
算法·leetcode·矩阵
依然鸣11 小时前
PTA团体程序设计天梯赛L1真题讲解L1-077-080
开发语言·c++·算法·深度优先·pat考试·图论
qeen8714 小时前
【数据结构】自平衡二叉搜索树各种旋转算法原理解析及AVL树的C++实现
数据结构·c++·算法
lucas_AI14 小时前
1.2B 小模型赢过 235B 大模型:NaviDC-OCR 把文档解析卷明白了
人工智能·深度学习·算法
冻柠檬飞冰走茶15 小时前
PTA基础编程题目集 7-35有理数均值(C++语言实现)
开发语言·数据结构·c++·算法·均值算法
民乐团扒谱机15 小时前
【微实验】倒谱算法(Cepstrum)深度解析:原理、数学推导与代码实现
人工智能·算法·语音识别
Nil20816 小时前
leetcode 189轮转数组
数据结构·算法·leetcode