1. 程式人生 > >python leetcode 260. Single Number III

python leetcode 260. Single Number III

關鍵是找到a和b第k位上一個為0一個為1,然後再與第k位上為1的異或

class Solution:
    def singleNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """ 
        a1=a2=0
        for n in nums:
            a1^=n 
        k = a1&(~a1+1)
        for n in nums:
            if n&
k: a2^=n return [a2,a1^a2]