LeetCode136:https://leetcode-cn.com/problems/single-number/submissions/
解题思路:参考题解,利用异或 。即将所有数字异或,然后相同的数字消除为0,剩下的即为所求。
1 class Solution: 2 def singleNumber(self, nums: List[int]) -> int: 3 res = 0 4 for i in nums: 5 res ^= i 6 return res
异或:设有a, b, c 三个数,则:
a ^ a = 0
a ^ 0 = a
a ^ b ^ c = a ^ c ^ b