1bit and 2bit 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))

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==bitsSize1) result= true; if((i==bitsSize2)&&bits[i]==1) result= false; if((i==bitsSize2)&&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 2bit Character along with its front element 1,or itself represents a 1bit 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 1bit Character and return true,otherwise return false.
if(bits.length==0  bits==null){
return false;
}
int pos = bits.length1;
int count = 0;
while(pos>0){
pos;
if(bits[pos]==1){
count++;
}else{
break;
}
}return count%2==0;