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:])
    }
}
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容