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

猜你喜欢

转载自www.cnblogs.com/ffeng0312/p/9227163.html