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]

 

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