717. 1比特与2比特字符

exiaohu 于 2022-02-20 发布

题目链接:717. 1比特与2比特字符

最后一位一定是一位字符有以下几种情况:

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])