异或运算的性质:

  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

 

相关文章:

  • 2021-07-28
  • 2022-12-23
  • 2021-07-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-04
猜你喜欢
  • 2021-10-25
  • 2021-07-25
  • 2021-06-10
  • 2021-06-11
  • 2021-06-27
  • 2021-06-19
  • 2021-11-25
相关资源
相似解决方案