Python O(N) time and O(1) Space Solution


  • 0
    U
    class Solution(object):
        def singleNumber(self, nums):
            """
            :type nums: List[int]
            :rtype: List[int]
            """
            first = 0
            second = 0
            xor = reduce(lambda x, y: x ^ y, nums)
            xor &= -xor
            for num in nums:
                if num & xor == 0:
                    first ^= num
                else:
                    second ^= num
            
            return [first, second]
                        
                
            
    

Log in to reply
 

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