169. 多数元素
list、set、str的转换:
list转set:set(list)
set转list:list(set)
注:list转set时会自动去重!
将list[str] digits转成list[int]:
法一:[ int(i) for i in digits ]
法二:map( int, digits )
将list[int] digits转成list[str]:
法一:[ str(i) for i in digits]
法二:map( str, digits )
注:法二语法:map(function,iterable,…)
1 class Solution(object): 2 def majorityElement(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: int 6 """ 7 # 转set去重 8 setnum = set(nums) 9 for i in setnum: 10 if nums.count(i) > int(len(nums) / 2): 11 return i 12 13 14 if __name__ == '__main__': 15 solution = Solution() 16 print(solution.majorityElement([3, 2, 3]))