169. 多数元素
最笨的方法
class Solution:
def majorityElement(self, nums: List[int]) -> int:
map = {}
re = []
for i in nums:
if i in map:
map[i] += 1
else:
map[i] = 1
re.append(i)
for i in re:
if map[i] > len(nums)/2:
return i
数学方法
class Solution:
def majorityElement(self, nums: List[int]) -> int:
nums.sort()
return(nums[len(nums) // 2])