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

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

Example 2:

Input:
bits = [1, 1, 1, 0]
Output: False

public static boolean isOneBitCharacter(int[] bits){
int i = 0;
while (i < bits.length-1){
if (bits[i] == 0){
i += 1;
continue;
}
if (bits[i] == 1){
i += 2;
continue;
}
}
return i == bits.length-1;
}

public static boolean isOneBitCharacter(int[] bits){
int i = bits.length-2;
while (i>=0&& bits[i] > 0){
i--;
}
return (bits.length - i) % 2 == 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120