-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathsolution.py
More file actions
22 lines (22 loc) · 685 Bytes
/
Copy pathsolution.py
File metadata and controls
22 lines (22 loc) · 685 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
# Time: O(n)
# Space: O(1)
def valid_utf8(self, data: list[int]) -> bool:
remaining = 0
for byte in data:
if remaining == 0:
if byte >> 7 == 0b0:
remaining = 0
elif byte >> 5 == 0b110:
remaining = 1
elif byte >> 4 == 0b1110:
remaining = 2
elif byte >> 3 == 0b11110:
remaining = 3
else:
return False
else:
if byte >> 6 != 0b10:
return False
remaining -= 1
return remaining == 0