Straight forward Python solution


  • 0
    A
        def countOnes(binary):
            i = 0
            while i<5 and binary[i]!='0':
                i += 1
            return i
        
        index = 0
        
        while index<len(data):
            binary = bin(data[index])[2:].zfill(8)
            numOnes = countOnes(binary)
            if numOnes==1 or numOnes>4:
                return False
            while numOnes>1:
                index += 1
                if index>=len(data) or countOnes(bin(data[index])[2:].zfill(8))!=1:
                    return False
                numOnes -= 1
            index += 1
        
        return True

Log in to reply
 

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