Easy to understand Java Solution 15 ms -extra space


  • 0
    C


    public class Solution {
    public boolean validUtf8(int[] data) {
    if(data==null || data.length==0) return true;
    int d[][]=new int[data.length][8];
    for(int i=0;i<data.length;i++){
    int index=7;
    while(data[i]!=0 && index>=0){
    d[i][index--]=data[i]&1;
    data[i]>>=1;
    }
    }
    int j=0;
    for(;j<d.length;j++){
    int count=0,i=0;
    for(i=0;i<8;i++){
    if(d[j][i]!=1)
    break;
    count++;
    }
    if(i==8) return false;
    if(count==0){
    continue;
    }
    int k=j+1;
    if(count==1) return false;
    if(count>0 && k>=d.length) return false;
    while(count>1 && k<d.length){
    count--;
    if(d[k][0]!=1 || d[k][1]!=0) return false;
    k++;
    }
    j=k-1;
    }
    return true;
    }


Log in to reply
 

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