文章目录
1. 题目
2. 思路
(1) 模拟法
- 遍历数组,若元素为1,则下标+2;否则,下标+1。
- 若下标最后落在最后一个元素上,则返回true。
3. 代码
public class Test {
public static void main(String[] args) {
}
}
class Solution {
public boolean isOneBitCharacter(int[] bits) {
int n = bits.length - 1;
int i = 0;
while (i < n) {
if (bits[i] == 1) {
i += 2;
} else {
i++;
}
}
return i == n;
}
}