Java One-liner recursive


  • 3
    class Solution {
        public boolean isOneBitCharacter(int[] bits) {
            return bits.length <= 2 ? bits[0] == 0 : isOneBitCharacter(Arrays.copyOfRange(bits, bits[0] + 1, bits.length));
        }
    }
    

    and 3-liner iterative.

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

  • 0
    H

    wow so elegant.


  • 1
    public boolean isOneBitCharacter(int[] bits) {
        return bits.length <= 2 ? bits[0] == 0 : isOneBitCharacter(Arrays.copyOfRange(bits, bits[0] + 1, bits.length));
    }

  • 0

    @ManuelP I didn't realize that there is no empty input. Good point.


  • 0
    C

    Very nice solution. Good catch that bits always end with a 0.


Log in to reply
 

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