题解 | #数组中只出现一次的数(其它数出现k次)#
数组中只出现一次的数(其它数出现k次)
http://www.nowcoder.com/practice/5d3d74c3bf7f4e368e03096bb8857871
# 整体来说比较简单 class Solution: def foundOnceNumber(self , arr: List[int], k: int) -> int: # write code here # 统计按位求与的结果 sum_list = [0] * 32 for num in arr: if num < 0: num = abs(num) sum_list[31] += 1 yu_result = bin(num & 2147483647)[2:][::-1] for i in range(len(yu_result)): if yu_result[i] == '1': sum_list[i] += 1 # 逐位对k求模,若为1,则是result的一部分 result = 0 for i in range(31): if sum_list[i] % k != 0: result += 2**i if sum_list[31] % k == 1: result *= -1 return result