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).
Now given 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.
class Solution { public boolean isOneBitCharacter(int[] bit) { int m = bit.length;//得到数组的长度 int i = 0; do{ if (bit[i] == 0) {
//当i位的数值为0的时候,这是一位字符
//i++
//遍历到最后剩余一位时,则为单字符串结尾,跳出循环 if(m - i == 1){ break; } i++; } else if (bit[i] == 1) { if(m - i == 2 ){ break; } i += 2; } }while (true); if (m - i == 1) { return true; } else { return false; } } }

浙公网安备 33010602011771号