717. 1-bit and 2-bit Characters
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.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.// Greedy + One scan
bool isOneBitCharacter(vector<int>& bits) { // time: O(n); space: O(1)
size_t n = bits.size(), i = 0;
while (i < n - 1) {
if (bits[i] == 1) {
i++;
}
++i;
}
return i == n - 1;
}Last updated