-
Notifications
You must be signed in to change notification settings - Fork 43
/
1-bit-and-2-bit-characters.py
62 lines (58 loc) · 1.49 KB
/
1-bit-and-2-bit-characters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# V0
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/79130681
class Solution(object):
def isOneBitCharacter(self, bits):
"""
:type bits: List[int]
:rtype: bool
"""
pos = 0
while pos < len(bits) - 1:
if bits[pos] == 1:
pos += 2
else:
pos += 1
return pos == len(bits) - 1 and bits[pos] == 0
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/79130681
class Solution(object):
def isOneBitCharacter(self, bits):
"""
:type bits: List[int]
:rtype: bool
"""
pos = 0
while pos < len(bits) - 1:
pos += 2 if bits[pos] == 1 else 1
return pos == len(bits) - 1
# V1''
# http://bookshadow.com/weblog/2017/10/29/leetcode-1-bit-and-2-bit-characters/
class Solution(object):
def isOneBitCharacter(self, bits):
"""
:type bits: List[int]
:rtype: bool
"""
size = len(bits)
c = 0
while c < size:
if c == size - 1: return True
if bits[c] == 0: c += 1
else: c += 2
return False
# V2
# Time: O(n)
# Space: O(1)
class Solution(object):
def isOneBitCharacter(self, bits):
"""
:type bits: List[int]
:rtype: bool
"""
parity = 0
for i in reversed(range(len(bits)-1)):
if bits[i] == 0:
break
parity ^= bits[i]
return parity == 0