题目链接:717. 1比特与2比特字符
最后一位一定是一位字符有以下几种情况:
- 序列只有一位;
- 序列的倒数第二位是
0; - 当后两位为
[1, 0]时,且序列的其余部分不能被成功解码时。
from typing import List
class Solution:
def decodable(self, bits: List[int]) -> bool:
if bits == [] or bits == [0] or bits == [1, 1] or bits == [1, 0]:
return True
if len(bits) >= 1 and bits[0] == 0 and self.decodable(bits[1:]):
return True
if len(bits) >= 2 and bits[0] == 1 and bits[1] == 1 and self.decodable(bits[2:]):
return True
if len(bits) >= 2 and bits[0] == 1 and bits[1] == 0 and self.decodable(bits[2:]):
return True
return False
def isOneBitCharacter(self, bits: List[int]) -> bool:
if len(bits) == 1:
return True
if bits[-2] == 0:
return True
return not self.decodable(bits[:-2])