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

posted @ 2018-06-26 06:01  ffeng0312  阅读(84)  评论(0)    收藏  举报