题目:
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋
times.
You may assume that the array is non-empty and the majority element always exist in the array.
题意:
给一个长度为n的数组,找出这个数组中出现次数大于n/2的数字
代码:
class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
dic = dict()
for x in nums :
dic[x] = 0
for x in nums :
dic[x] = dic[x] + 1
for x in dic :
if dic[x] > len(nums)/2 :
return x
笔记:用的是Python里面的字典数据结构来做