229. Majority Element II

Posted ffeng0312

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了229. Majority Element II相关的知识,希望对你有一定的参考价值。

class Solution(object):
  def majorityElement(self, nums):
  """
  :type nums: List[int]
  :rtype: int
  """
    n=len(nums)
    a=n/3
    freq={}
    result=[]
    for i in nums:
      if i not in freq:
        freq[i]=1
      else:
        freq[i] +=1
        if freq[i]>a and i not in result:
          result.append(i)
    if result==[] and n<3:
      result=nums
    return result


















以上是关于229. Majority Element II的主要内容,如果未能解决你的问题,请参考以下文章

229. Majority Element II

229. Majority Element II

LeetCode 229: Majority Element II

229. Majority Element II

229. Majority Element II

229. Majority Element II