leetcode 2020年3月 每日一题打卡
思路: python
细节:
查找list中某元素的位置:list.index(i) 向下取整:int()代码:
class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
tag=[]
times=[]
id_tag=0
for i in nums:
if i in tag:
times[tag.index(i)]+=1
else:
tag.append(i)
times.append(1)
id_tag+=1
s=int(len(nums)/2)
ans=0
best=0
for i in range(0,id_tag):
if times[i]>best and times[i]>s:
ans=tag[i]
best=times[i]
return ans
如转载请标明出处!侵权必究