gpt4 book ai didi

717. 1-bit and 2-bit Characters 1 比特与 2 比特字符

转载 作者:大佬之路 更新时间:2024-01-31 14:18:07 25 4
gpt4 key购买 nike

题目地址:https://leetcode.com/problems/1-bit-and-2-bit-characters/description/open in new window

题目描述

Wehave 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).

Nowgiven 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:

Input: 
bits = [1, 0, 0]
Output: True
Explanation: 
The only way to decode it is two-bit character and one-bit character. So the last character is one-bit character.

Example 2:

Input: 
bits = [1, 1, 1, 0]
Output: False
Explanation: 
The only way to decode it is two-bit character and two-bit character. So the last character is NOT one-bit character.

Note:

1、 1<=len(bits)<=1000.;
2、 bits[i]isalways0or1.;

题目大意

有两种字符,一种是0,一种是10或者11,现在要判断整个数组是否由这两种组成的,要求最后一位的数字必须是单个的0.

解题方法

遍历

这个题真的很简单,因为有两种字符串,一种是0,一种是10或11。即一种长度是1,一种长度是2. 所以找个指针然后遍历一遍,看看当前值是0还是1,是0走1步,是1走两步。最后如果能到达len-1即可。

下面是第一次提交,想到了判断最后一个字符是不是0。题目中已经明确告诉了是0,所以下面有个改进版的。

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14

精简:

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

1 2 3 4 5 6 7 8 9 10

DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有

本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com