393. UTF-8 Validation | English | Medium

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

class Solution:
def validUtf8(self, data: List[int]) -> bool:
def countOnes(num):
count = 0
for i in range(7, -1, -1): # = 1 << 7
if num & (1 << i):
count += 1
else:
break
return count
count = 0
for d in data:
if not count:
count = countOnes(d)
if count == 0:
continue
if count == 1 or count > 4:
return False
count -= 1
else:
count -= 1
if countOnes(d) != 1:
return False
return count == 0

Runtime: 116 ms, faster than 75.78% of Python3 online submissions for UTF-8 Validation.
Memory Usage: 14.5 MB, less than 17.39% of Python3 online submissions for UTF-8 Validation.

aunnfriends
join shbcf.ru