Python | Leetcode Python题解之第229题多数元素II

题目:

题解:

python 复制代码
class Solution:
    def majorityElement(self, nums: List[int]) -> List[int]:
        cnt = {}
        ans = []

        for v in nums:
            if v in cnt:
                cnt[v] += 1
            else:
                cnt[v] = 1
        for item in cnt.keys():
            if cnt[item] > len(nums)//3:
                ans.append(item)

        return ans
相关推荐
ZZHow102418 小时前
02OpenCV基本操作
python·opencv·计算机视觉
计算机学长felix19 小时前
基于Django的“酒店推荐系统”设计与开发(源码+数据库+文档+PPT)
数据库·python·mysql·django·vue
站大爷IP19 小时前
Python随机数函数全解析:5个核心工具的实战指南
python
悟乙己19 小时前
使用 Python 中的强化学习最大化简单 RAG 性能
开发语言·python·agent·rag·n8n
max50060019 小时前
图像处理:实现多图点重叠效果
开发语言·图像处理·人工智能·python·深度学习·音视频
AI原吾19 小时前
玩转物联网只需十行代码,可它为何悄悄停止维护
python·物联网·hbmqtt
云动雨颤19 小时前
Python单元测试入门:3个核心断言方法,帮你快速定位代码bug
python·单元测试
SunnyDays101120 小时前
Python 实现 HTML 转 Word 和 PDF
python·html转word·html转pdf·html转docx·html转doc
共享家952720 小时前
优先搜索(DFS)实战
算法·leetcode·深度优先
跟橙姐学代码20 小时前
Python异常处理:告别程序崩溃,让代码更优雅!
前端·python·ipython