题目:
题解:
class Solution:def validUtf8(self, data: List[int]) -> bool:MASK1, MASK2 = 1 << 7, (1 << 7) | (1 << 6)def getBytes(num: int) -> int:if (num & MASK1) == 0:return 1n, mask = 0, MASK1while num & mask:n += 1if n > 4:return -1mask >>= 1return n if n >= 2 else -1index, m = 0, len(data)while index < m:n = getBytes(data[index])if n < 0 or index + n > m or any((ch & MASK2) != MASK1 for ch in data[index + 1: index + n]):return Falseindex += nreturn True