Single Number
题目描述
Given an array of integers, every element appears twice except for one. Find that single one.
Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
解题方法
记录每个bit的出现次数
用一个array或者hashmap记录每个bit 1的出现次数,如果是奇数次就是single number里的
XOR
Solution
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
result = 0
for num in nums:
result ^= num
return result