给定一个整型数组,找出主元素,它在数组中的出现次数严格大于数组元素个数的二分之一。2017.11.17 (就是出现次数最多的数)它的说明并不对
class Solution:
"""
@param: nums: a list of integers
@return: find a majority number
"""
def majorityNumber(self, nums):
# write your code here
mmap = {}
for i in nums:
if i in mmap:
mmap[i]+=1
else:
mmap[i] = 1
mmax = 0
maxkey = ''
#print(mmap)
for key,value in mmap.items():
if mmax < value:
mmax = value
maxkey = key
return maxkey
so easy. 2017.11.17
网友评论