Scan backwards


  • 0

    The idea is to find the index of the second last zero (if there is only one zero, the index is -1) by scanning the array backwards. Then we calculate how many ones between the second last and the last zeros. If the number is odd, return false, otherwise, true:

    public boolean isOneBitCharacter(int[] bits) {
        int l = bits.length - 2;
        while(l >= 0 && bits[l] == 1) l--;
        return (bits.length - l) % 2 != 1;
    }

Log in to reply
 

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