leetcode-137-只出现一次的数字②
题目描述:

方法一:数学
class Solution: def singleNumber(self, nums: List[int]) -> int: return (sum(set(nums))*3 - sum(nums))//2
方法二:
class Solution: def singleNumber(self, nums: List[int]) -> int: hash_table = {} for i in nums: if i not in hash_table: hash_table[i] = 1 else: hash_table[i] +=1 for i,v in hash_table.items(): if v == 1: return i
方法三:位运算*
class Solution: def singleNumber(self, nums: List[int]) -> int: res = 0 for i in range(32): cnt = 0 bit = 1 << i for num in nums: if num & bit != 0: cnt += 1 if cnt % 3 != 0: res |= bit return res - 2**32 if res > 2**31-1 else res
浙公网安备 33010602011771号