Simple Java solution, based on State machine


  • 0
    A

    4 different states: 0: success; 1: require at least one more bit(false); 2: false; 3: ambiguous(false)

    class Solution {
        public boolean isOneBitCharacter(int[] bits) {
            int state=0; 
            for(int i=0;i<bits.length;i++){
                if(state==0){
                    state=bits[i];
                }
                else if(state==1){
                    state<<=1;
                    state+=bits[i];
                }
                else {
                    state=bits[i];   
                }
            }
            return state==0;
        }
    }
    

Log in to reply
 

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