leetcode 查找每个元素都出现两次的列表,返回只出现一次的元素

Given an array of integers, every element appears # twice except for one. Find that single one.

class Solution(object):
    def singleNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        xor = 0
        for num in nums:
            xor ^= num
        return xor
x=Solution()
print(x.singleNumber([2,5,2,6,7,6,7]))

 输出:

5

 

posted @ 2019-03-20 20:02  anobscureretreat  阅读(410)  评论(0编辑  收藏  举报