Fun Python Solution


  • 1
    M

    Kinda slow but was fun to make and see working.

    def gBin(n,prec=32):
        bin=[0]*prec
        for i in range(prec):
            bin[-1-i]=n&1
            n=n>>1
        return bin
    
    def TXOR(B1,B2):
        return [(a + b)%3 for a, b in zip(B1, B2)]
        
    def gInt(bin):
        prec=len(bin)
        intg=0
        sgn=1
    
        if bin[0]==1:
            bin=[1-x for x in bin]
            sgn=-1
    
        for i in range(prec-1):
            intg+=bin[-i-1]*2**(i)
        
        return sgn*intg+(sgn-1)/2
    
    class Solution:
        # @param A, a list of integer
        # @return an integer
        def singleNumber(self, A):
            x=gBin(0)
            for a in A:
               x=TXOR(x,gBin(a))
            return gInt(x)

Log in to reply
 

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