Using bits


  • 0

    '''
    class Solution {
    public boolean validUtf8(int[] data) {
    String [] input = new String [data.length];
    for (int i =0; i< data.length; i++)
    {
    input[i] = String.format("%8s",Integer.toBinaryString(data[i] & 0xFF)).replace(' ', '0');
    System.out.println(input[i]);
    }
    int start =0;
    int groupCount =0;
    while(start<data.length)
    {
    groupCount = createHroup(input[start]);
    if(groupCount == 0)
    {
    if(validate1Byte(input[start]))
    start++;
    else
    return false;
    }
    else if(groupCount == 1) return false;
    else{
    start++;
    for (int i= 2; i<=groupCount; i++)
    {
    if (start >= data.length)
    return false;
    if(!validate10(input[start]))
    return false;
    start++;
    }

            }
        }
        return true;       
    }
    
    public boolean validate1Byte(String firstData)
    {
        if(firstData.charAt(0) == '0')
            return true;
        return false;
    }
    public boolean validate10(String firstData)
    {
        if(firstData.charAt(0) == '1' && firstData.charAt(1) == '0')
            return true;
        return false;
    }
    
    public int createHroup(String firstData)
    {
         int count = 0;
        
        for (char ch : firstData.toCharArray())
        {
            if (ch == '0')
                return count;
            else
                count++;
        }
        
       return count;
    }
    

    }
    '''


Log in to reply
 

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