Loading...

10-05 leetcode 0229

链接 229. Majority Element II

题目

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times.

题解

没啥好说的,直接写

1
2
3
4
5
6
7
8
from collections import Counter


class Solution:
def majorityElement(self, nums: list[int]) -> list[int]:
counter = Counter(nums)
flag = len(nums) // 3
return [key for key, value in counter.items() if value > flag]

Comment