python solution, nothing special...


  • 0
    class Solution(object):
        def convert_i_2_str(self, n):
            d = str(bin(n)[2:])
            return (8-len(d))*'0'+d
        def validUtf8(self, data):
            sdata = [self.convert_i_2_str(d) for d in data]
            i = 0
            while i<len(sdata):
                if sdata[i].startswith("0"):#For 1-byte character
                    i += 1
                else:
                    m = sdata[i].find("0")#For n-bytes character
                    if m==1:
                        return False
                    if m==-1:
                        m = 8
                    if i+m>len(sdata):
                        return False
                    for j in range(i+1, i+m):
                        if not sdata[j].startswith("10"):
                            return False
                    i += m
            return True
    

Log in to reply
 

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