[LeetCode] 1-bit and 2-bit Characters

717. 1-bit and 2-bit Characters

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.

1
2
3
4
5
6
7
8
9
10
class Solution {
public:
bool isOneBitCharacter(vector<int>& bits) {
int i = bits.size() - 2;
while (i >= 0 && bits[i] > 0) {
i--;
}
return (bits.size() - i) % 2 == 0;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/11/18/PS/LeetCode/1-bit-and-2-bit-characters/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.