A/C Python solution, easy to understand, beat 7%

• 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"
continue
elif curFirstZero == 1:
#print "one 1 in beginning of string"
flagValid = False
break
elif curFirstZero > 4:
#print "more than 4 bytes"
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``````

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