class Solution:
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
can1=0.1
count1=0
can2=0.2
count2=0
for n1 in nums:
if n1==can1:
count1+=1
elif n1==can2:
count2+=1
elif count1==0:
count1=1
can1=n1
elif count2==0:
count2=1
can2=n1
else:
count1-=1
count2-=1
count1=count2=0
for n1 in nums:
if n1==can1:
count1+=1
elif n1==can2:
count2+=1
if count1 > len(nums)/3 and count2 > len(nums)/3:
return [can1,can2]
elif count1 > len(nums)/3:
return [can1]
elif count2 > len(nums)/3:
return [can2]
else:
return []
python leetcode 229. Majority Element II
最新推荐文章于 2022-03-31 10:12:56 发布