【Leetcode_easy】717. 1-bit and 2-bit Characters

阅读 85

2022-07-12

problem

​​717. 1-bit and 2-bit Characters​​

题意:
solution1:

class Solution {
public:
bool isOneBitCharacter(vector<int>& bits) {
int i=0;
while(i<bits.size()-1)
{
if(bits[i]==0) i+=1;
else i+=2;
}
return i==bits.size()-1;
}
};

solution2:根据数组的特性计算。

class Solution {
public:
bool isOneBitCharacter(vector<int>& bits) {
int i=0, n = bits.size();
while(i<n-1)
{
//if(bits[i]==0) i+=1;
//else i+=2;
i += bits[i]+1;
}
return i==n-1;
}
};

 

参考

1. ​​Leetcode_easy_717. 1-bit and 2-bit Characters​​;

2. ​​Grandyang​​;

精彩评论(0)

0 0 举报