# 1-bit and 2-bit Characters

• A few variations of approach #2:

Using `index`:

``````    def isOneBitCharacter(self, bits):
return not (bits[-2::-1] + [0]).index(0) % 2
``````

Using `iter`:

``````    def isOneBitCharacter(self, bits):
r = reversed(bits).next
return not sum(iter(r, r())) % 2
``````

Using `takewhile`:

``````from itertools import takewhile

class Solution:
def isOneBitCharacter(self, bits):
it = reversed(bits)
return not sum(takewhile(int, it), next(it)) % 2
``````

Using `reduce`:

``````from operator import xor
from itertools import takewhile

class Solution:
def isOneBitCharacter(self, bits):
it = reversed(bits)
return not reduce(xor, takewhile(int, it), next(it))
``````

• while (i<len) {
if (a[i]) {
i+=2;
isOneChar = 0;
} else {
i++;
isOneChar=1;
}
}

• from functools import reduce
return reduce(lambda x, y: 0 - y if x >= 0 else 1, bits, 0) == 0

• seems my solution is too overcomplicated... QAQ

``````bool isOneBitCharacter(vector<int>& bits) {
vector<int> new_vec = bits;
while (!new_vec.empty() || new_vec.size() == 1) {
if (new_vec[0] == 1 && new_vec.size() > 1) {
new_vec.erase(new_vec.begin());
new_vec.erase(new_vec.begin());
continue;
}
else if (new_vec[0] == 0 && new_vec.size() > 1) {
new_vec.erase(new_vec.begin());
continue;
} else {
break;
}
}
if (new_vec.empty()) {
return false;
} else return new_vec[0] == 0;
}
``````

• bool isOneBitCharacter(int* bits, int bitsSize) {
int i=0;
bool result;
if(bitsSize==1)
return true;
while(i<bitsSize)
{

``````    if(i==bitsSize-1)
result= true;
if((i==bitsSize-2)&&bits[i]==1)
result= false;
if((i==bitsSize-2)&&bits[i]==0)
result= true;
if(bits[i]==1)
i+=2;
if(bits[i]==0)
i+=1;
}
return result;
``````

}

• There is no need to iterate through the whole array. Whenever there is a 0, it means this 0 belongs to a 2-bit Character along with its front element 1,or itself represents a 1-bit Character. So we could just count the number of 1 before the last 0, if the count is an even number, than the last 0 itself represents a 1-bit Character and return true,otherwise return false.
if(bits.length==0 || bits==null){
return false;
}
int pos = bits.length-1;
int count = 0;
while(pos>0){
pos--;
if(bits[pos]==1){
count++;
}else{
break;
}
}

``	 return count%2==0;``

• class Solution {
public boolean isOneBitCharacter(int[] bits) {
for(int i=0;i< bits.length;i++){
if(bits[i] == 0) continue;
if(i >= bits.length - 2) return false;
i++;
}
return true;
}
}

• JS
/**

• @param {number[]} bits
• @return {boolean}
*/
var isOneBitCharacter = function(bits) {
var str=bits.join("");
var r=/10|11|0/;
while(r.test(str)&&str!='0'){
str=str.replace(r,"");
}
return str=='0'?true:false;
};

• #Python3.6
class Solution:
def isOneBitCharacter(self, bits):
"""
:type bits: List[int]
:rtype: bool
"""
parity=0
lb=len(bits)
for i in range(lb-2,-1,-1):
if bits[i]==0:
break
parity^=1
return parity==0

• doesn't approach #2 fail for 10,11,11,1? last is not a zero but it will incorrectly return true. Unless you add an intial check for bits[bits.length-1] != 0 and return false if its set to 1 as a trivial case

• Ah. Nevermind; I just reread the problem and it states "The given string will always end with a zero".

• If second element from last is 0: it is always true
if second element from last is 1: count the number of continuous 1's before last zero.
if the count turns out to be odd--false
otherwise--true

CODE:

bool isOneBitCharacter(vector<int>& bits) {
int count=0;
int length=bits.size();
int i=length-2;
if(bits[i]==0)
return true;
else{
while(i>=0 && bits[i]==1){
count=count+1;
i=i-1;
}
if(count%2==0)
return true;
else
return false;
}

``    }``

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.