Concise Python Solution


  • 0
    W
    class Solution(object):
    
        def singleNumber(self, nums):
            from operator import xor
            a_xor_b = reduce(xor, nums)
            
            k = 0
            tmp = a_xor_b
            while tmp & 1 == 0:
                k += 1
                tmp >>= 1
    
            a = reduce(xor, (n for n in nums if (n >> k) & 1 == 1))
            return [a, a_xor_b ^ a]
    

  • 0
    R

    You used list comprehension in

    a = reduce(xor, [n for n in nums if (n >> k) & 1 == 1])

    Will it then use O(n) space?


  • 0
    W

    yes it will, should change it to a generator: a = reduce(xor, (n for n in nums if (n >> k) & 1 == 1))


Log in to reply
 

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