只出现一次的数字
https://leetcode-cn.com/problems/single-number/ (opens new window) 位运算,用到了异或 对于异或,需要记住的两大特性 0 异或任何数都是它本身 任何数异或它本身等于0
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
"""
位运算,异或
0 异或任何数都是它本身
任何数异或它本身等于0
"""
ans = 0
for i in nums:
ans ^= i
return ans