Python Bit Manipulation (with more general case)


  • 10
    B
    class Solution(object):
        def singleNumber(self, nums):
            one, two = 0, 0
            for x in nums:
                one, two, three = one ^ x, two | (one & x), two & x
                one, two = one & ~three, two & ~three
            return one
    

    Actually, this approach can be generalized for the case that each number appears 5 times except one:

    class Solution(object):
        def singleNumber(self, nums):
            one = two = three = four = 0
            for x in nums:
                one, two, three, four, five = one ^ x, two | (one & x), three | (two & x), four | (three & x), four & x
                one, two, three, four = one & ~three & ~five, two & ~three, three & ~four, four & ~five
            return one
    

    If each number appears 5 times except that one number appears only 3 times, return three will be the result


Log in to reply
 

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