27/49 pass, Python, easy to understand, don't understand why case [250, 145, 145, 145, 145] need return false


  • 0
    W

    Failed for case [250, 145, 145, 145, 145], expect false, my code return true.

    Don't understand why this case is false.

    class Solution(object):
    def validUtf8(self, data):
    """
    :type data: List[int]
    :rtype: bool
    """
    print "inputed data = ", data
    numData = len(data)
    print "numData = ", numData

        if numData < 1:
            return False
    
        binData = []
        for d in data:
            binData.append(str('{:08b}'.format(d)))
        # binData = bin(data)
        #
        print "binData = ", binData
    
        def detectOnes(string):
            #print "detectOnes string = ", string
    
            try:
                firstZero = string.index('0')
            except:
                firstZero = 8
            #print "firstZero = ", firstZero
    
            numStartOnes = firstZero
            #print "numStartOnes = ", numStartOnes
    
            return numStartOnes
    
        flagValid = True
        while len(binData) > 0 and flagValid == True:
            curBinData = binData.pop(0)
            print "curBinData = ", curBinData
    
            curFirstZero = detectOnes(curBinData)
            print "curFirstZero = ", curFirstZero
    
            if curFirstZero == 0:
                print "start with 0, must be a unicode code"
            elif curFirstZero == 1:
                print "one 1 in beginning of string"
                flagValid = False
                break
            elif curFirstZero == 8:
                print "something is wrong, not valid"
                flagValid = False
                break
            else:
                print "need more check"
                for i in range(1, curFirstZero):
                    if len(binData) < 1:
                        flagValid = False
                        break
    
                    newCurBinData = binData.pop(0)
                    print "newCurBinData = ", newCurBinData
                    if detectOnes(newCurBinData) != 1:
                        flagValid = False
                        break
    
    
        print "return flagValid = ", flagValid
        return flagValid

Log in to reply
 

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