-
标签:
位运算
-
难度:
简单
- 题目描述

- 我的解法
利用散列表,此题没有多大难度。注意 Python
中获取字典中 value
的最大值所对应的键的方法: max(dict, key = dict.get)
, 如果直接使用 max(dict)
,则返回的是字典中键值最大的键。
a = {1:2, 2:1}
max() # 返回 2
max(a, key = a.get ) # 返回 1
class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
s = {}
for i in nums:
if(s.get(i, 0)==0):
s[i] = 1
else:
s[i] += 1
return max(s,key=s.get)
- 其他解法
暂略。
网友评论