首页 > 其他 > 详细

717. 1-bit and 2-bit Characters

时间:2020-07-15 01:42:32      阅读:61      评论:0      收藏:0      [点我收藏+]

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

 

717. 1-bit and 2-bit Characters

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!