Java Solution with explanation (8ms)


  • 0
    T
    public boolean isOneBitCharacter(int[] bits) {
        int n=bits.length;
        if(n==1) return true;//only one-bits
        if(bits[n-1]==1) return false;//last bits 1, can only be 11.
        //between two 0s, if the number of '1' is even,  then last '0' is one-bit; otherwise two-bits;
        //0,111,0 two bits;  0, 1111,0; one-bits; the string before first '0' can be ignored.
        int oneCount=0;
        for(int j=2;j<n+1;j++){
            if(bits[n-j]==0){
                if(oneCount%2==0) return true; 
                else return false;
            }else{
                oneCount++;
            }
        }
        //when no more '0' in the bits.
        if(oneCount%2==0) return true;
        return false;
    }

Log in to reply
 

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