717. 1-bit and 2-bit Characters

时间:2020-07-15 01:42:32   收藏:0   阅读:59

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

 

原文:https://www.cnblogs.com/whatyouthink/p/13302321.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!