一個非零數字 XOR 另外一個數字兩字,會變回自己
a ⊕ b ⊕ b = a
因為一個數字 XOR 兩次,會變 0,且任何數 XOR 0 會等於自己
class Solution:
def singleNumber(self, nums: List[int]) -> int:
res = 0
for n in nums:
res ^= n
return res
Time O(N)
Space O(1)