leetcode 225 Implement Stack using Queues queue stack
leetcode 2020 March 1 question daily punch
Ideas: Python
detail:
- Find a list of elements in a position: list.index (i)
- Rounding down: int ()
Code:
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
As reproduced, please indicate the source! All Rights Reserved