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

题目:

题解:

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

相关推荐

  1. 多数元素算法(leetcode169)

    2024-07-12 09:44:02       49 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 09:44:02       51 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 09:44:02       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 09:44:02       44 阅读
  4. Python语言-面向对象

    2024-07-12 09:44:02       55 阅读

热门阅读

  1. springmvc-03

    2024-07-12 09:44:02       18 阅读
  2. 大模型论文、github地址汇总

    2024-07-12 09:44:02       22 阅读
  3. 笔记小结:Softmax回归之模块导入与数据加载

    2024-07-12 09:44:02       25 阅读
  4. 视频调整帧率、分辨率+音画同步

    2024-07-12 09:44:02       24 阅读
  5. Linux - VIM 全面教程

    2024-07-12 09:44:02       21 阅读
  6. Three 圆柱坐标(Cylindrical)和 视锥体(Frustum)

    2024-07-12 09:44:02       29 阅读
  7. Emacs有什么优点,用Emacs写程序比IDE更方便吗?

    2024-07-12 09:44:02       26 阅读