O(1) constant space solution with comment


  • 2
    Y
    class Solution(object):
        def singleNumber(self, nums):
            """
            :type nums: List[int]
            :rtype: List[int]
            """ 
            a,res=0,[0,0]
            '''
            If the two numbers are m and n, the next few lines
            will get the least significant bit of 1 in m xor n.
            One of them has 1 on that bit and  the other one has 
            0 on that bit.
            '''
            for x in nums:
                a^=x
            a=a&(-a)
            '''
            Divide the numbers into two part by the value on 
            that bit, and xor those numbers
            '''
            for x in nums:
                res[x&a==0]^=x
            return res

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.