Java solution


  • 0
    J

    '''
    public static boolean validUtf8(int[] data) {

        int count = 0;
    
        for(int n: data) {
    
            if(count > 0) {
    
                if(n >= (2 << 6) && n < (3 << 6)) count--;
    
                else return false;
    
            } else {
    
                if(n < (1 << 7)) continue;
    
                if(n >= (30 << 3) && n < (31 << 3)) count = 3;
    
                else if(n >= (14 << 4) && n < (15 << 4)) count = 2;
    
                else if(n >= (6 << 5) && n < (7 << 5)) count = 1;
    
                else return false;
    
            }
    
        }
    
        if(count == 0) return true;
    
        return false;
    
    }
    

    '''
    See if current number lands between [11110000, 11110111] or [1110000, 11101111], or [11000000, 11011111], or [00000000, 01111111] to decide the count of following numbers starting with '10' (numbers within the range [10000000, 10111111]).


Log in to reply
 

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