Leetcode 136. Single Number

异或运算的性质:

  1. 0^a=a
  2. a^a=0
  3. 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

 

posted @ 2019-03-11 03:38  周洋  阅读(108)  评论(0编辑  收藏  举报