-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsolution.py
52 lines (49 loc) · 1.25 KB
/
solution.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
from collections import deque
class Solution(object):
def isOneBitCharacter_1(self, bits):
"""
:type bits: List[int]
:rtype: bool
"""
"""
time complexity: O(n^2)
space complexity: O(n)
"""
while len(bits) > 1:
if bits[0]:
bits = bits[2:]
else:
bits = bits[1:]
return bits == [0]
class Solution(object):
def isOneBitCharacter_2(self, bits):
"""
:type bits: List[int]
:rtype: bool
"""
"""
using queue
time complexity: O(n)
space complexity: O(n)
"""
bits_queue = deque(bits)
while len(bits_queue) > 1:
if bits_queue[0]:
bits_queue.popleft()
bits_queue.popleft()
else:
bits_queue.popleft()
return bits_queue == deque([0])
class Solution(object):
def isOneBitCharacter_3(self, bits):
"""
:type bits: List[int]
:rtype: bool
"""
length = 0
while length < len(bits) - 1:
if bits[length]:
length += 2
else:
length += 1
return length == len(bits) - 1