python straightforward solution


  • 0
    L
    class Solution(object):
        def validUtf8(self, data):
            """
            :type data: List[int]
            :rtype: bool
            """
            i = 0
            while i < len(data):
                count = 0
                mask = 1 << 7
                while data[i] & mask:
                    count += 1
                    mask >>= 1
                if count == 0:
                    i += 1
                    continue
                if count == 1:
                    return False
                while count > 1:
                    i += 1
                    if i > len(data) - 1 or not data[i] & (1 << 7) or data[i] & (1 << 6):
                        return False
                    count -= 1
                i += 1
            return True
    

Log in to reply
 

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