LeetCode Solutions
717. 1-bit and 2-bit Characters
Time: $O(n)$ Space: $O(1)$
class Solution {
public:
bool isOneBitCharacter(vector<int>& bits) {
const int n = bits.size();
int i = 0;
while (i < n - 1)
if (bits[i] == 0)
i += 1;
else
i += 2;
return i == n - 1;
}
};
class Solution {
public boolean isOneBitCharacter(int[] bits) {
final int n = bits.length;
int i = 0;
while (i < n - 1)
if (bits[i] == 0)
i += 1;
else
i += 2;
return i == n - 1;
}
}
class Solution:
def isOneBitCharacter(self, bits: List[int]) -> bool:
i = 0
while i < len(bits) - 1:
i += bits[i] + 1
return i == len(bits) - 1