LeetCode 717

Problem Description

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.

Example:

[1, 0, 0] => True
[1, 0, 1, 0] => False

Note:

1 <= len(bits) <= 1000.

bits[i] is always 0 or 1.

Python:


class Solution(object):

    def isOneBitCharacter(self, bits):

        """

        :type bits: List[int]

        :rtype: bool

        """

        return True if len(bits) == 1 else False if len(bits) == 0 else self.isOneBitCharacter(bits[ 1 if bits[0] == 0 else 2 :])

Java:

class Solution {
    public boolean isOneBitCharacter(int[] bits) {
        
        boolean isOne = false;
        
        for(int i = 0; i < bits.length; i++) {
            if(bits[i] == 1) {
                isOne = false;
                i++;
            } else {
                isOne = true;
            }
        }
        
        return isOne;
        
    }
}

Go:

func isOneBitCharacter(bits []int) bool {
    if len(bits) == 0 {
        return false
    } else if len(bits) == 1 {
        return true
    } else if bits[0] == 1{
        return isOneBitCharacter(bits[2:])
    } else {
        return isOneBitCharacter(bits[1:])
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容