Finite state machine with two states


  • 0
    H

    two states: 0, 1
    three edges: 0 -> 0 (read 0), 0 -> 1 (read 1), 1 -> 0 (read 0 or 1)
    code

    class Solution {
        public boolean isOneBitCharacter(int[] bits) {
            int state = 0, prev = 0;
            for (int b : bits) {
                prev = state;
                state = state == 1 ? 0 : b;
            }
            return state == 0 && prev == 0;
        }
    }
    

Log in to reply
 

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