class Solution:
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
# method one 运用set()特性
# for i in set(nums):
# if nums.count(i) * 2 > len(nums):
# return i
# method two 排序中位数
return sorted(nums)[ len(nums) // 2 ]
leetcode 169. 求众数(Majority Element) python3 多种思路( 利用set() / sorted()一行解决 )
猜你喜欢
转载自blog.csdn.net/huhehaotechangsha/article/details/80542323
今日推荐
周排行