返回介绍

solution / 0700-0799 / 0717.1-bit and 2-bit Characters / README_EN

发布于 2024-06-17 01:03:35 字数 2715 浏览 0 评论 0 收藏 0

717. 1-bit and 2-bit Characters

中文文档

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

Given a binary array bits that ends with 0, return true if the last character must be a one-bit character.

 

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.

 

Constraints:

  • 1 <= bits.length <= 1000
  • bits[i] is either 0 or 1.

Solutions

Solution 1

class Solution:
  def isOneBitCharacter(self, bits: List[int]) -> bool:
    i, n = 0, len(bits)
    while i < n - 1:
      i += bits[i] + 1
    return i == n - 1
class Solution {
  public boolean isOneBitCharacter(int[] bits) {
    int i = 0, n = bits.length;
    while (i < n - 1) {
      i += bits[i] + 1;
    }
    return i == n - 1;
  }
}
class Solution {
public:
  bool isOneBitCharacter(vector<int>& bits) {
    int i = 0, n = bits.size();
    while (i < n - 1) i += bits[i] + 1;
    return i == n - 1;
  }
};
func isOneBitCharacter(bits []int) bool {
  i, n := 0, len(bits)
  for i < n-1 {
    i += bits[i] + 1
  }
  return i == n-1
}
/**
 * @param {number[]} bits
 * @return {boolean}
 */
var isOneBitCharacter = function (bits) {
  let i = 0;
  const n = bits.length;
  while (i < n - 1) {
    i += bits[i] + 1;
  }
  return i == n - 1;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文