Simple recursive solution


  • 0
    J

    Based on the observation that if bits[idx] == 1 then we must use 2 bits, so advance idx by 2

        public boolean isOneBitCharacter(int[] bits) {
            return isOneBit(bits, 0);
        }
        
        private boolean isOneBit(int[] bits, int idx) {
            if (idx == bits.length - 1 && bits[idx] == 0) {
                return true;
            }
            if (idx >= bits.length) {
                return false;
            }
            
            if (bits[idx] == 1) {
                return isOneBit(bits, idx + 2);
            } else {
                return isOneBit(bits, idx + 1);
            }
        }
    

Log in to reply
 

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