Leetcode 136. Single Number
异或运算的性质:
- 0^a=a
- a^a=0
- a^b^a=a^a^b=0^b=b
所以只需要把全体数字异或运算一遍即可.
class Solution(object): def singleNumber(self, nums): """ :type nums: List[int] :rtype: int """ ans=nums[0] for a in nums[1:]: ans^=a return ans
异或运算的性质:
所以只需要把全体数字异或运算一遍即可.
class Solution(object): def singleNumber(self, nums): """ :type nums: List[int] :rtype: int """ ans=nums[0] for a in nums[1:]: ans^=a return ans