Leetcode 260. Single Number III

异或过一遍得到ans是两个数的异或.

然后就想办法分开两个数.

方法是找到第一个不一样的位,然后把整体分两组算一下.

class Solution(object):
    def singleNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        ans=0
        for num in nums:
            ans^=num
        mark=1
        while not (mark&ans): mark<<=1
        a,b=0,0
        for num in nums:
            if not mark&num:
                a^=num
            else:
                b^=num
        return [a,b]
原文地址:https://www.cnblogs.com/zywscq/p/10508429.html