git
list轉set:set(list)spa
set轉list:list(set)code
注:list轉set時會自動去重!blog
法一:[ int(i) for i in digits ]it
法二:map( int, digits )io
法一:[ str(i) for i in digits]function
法二:map( str, digits )class
注:法二語法:map(function,iterable,…)object
List
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]))