717. 1-bit and 2-bit Characters

We have two special characters. The first character can be represented by one bit 0. The second character can be represented by two bits (10 or 11).

Now given a string represented by several bits. Return whether the last character must be a one-bit character or not. The given string will always end with a zero.

有两种字符,第一种是单个0第二种是10或者11,问给一个01数组,,最后结尾是不是第一种字符。

从0开始遍历数组,直到len(bits) - 2,如果当前位是1,那就i+=2否则i+1,最后看i是否还剩余最后一位。

100->i = 2 true

1110->i=4 false

class Solution(object):
    def isOneBitCharacter(self, bits):
        """
        :type bits: List[int]
        :rtype: bool
        """
        i = 0
        while i < len(bits) - 1:
            if bits[i] == 1:
                i += 1
            i += 1
        return i == len(bits) - 1

 

posted @ 2020-07-14 22:33  whatyouthink  阅读(96)  评论(0编辑  收藏  举报