beat 90%


  • 0
    public class Solution {
    	    public boolean validUtf8(int[] data) {
    	        int len = 0;
    	        for(int n : data){
    	        	if(len == 0){
    	        		//count this data most significant bits
    	        		if(n >= 240 && n < 248) len = 3; // 4 *1
    	        		else if(n >= 224) len = 2;//3*1
    	        		else if(n >= 192) len = 1;//2*1
    	        		else if(n < 128) {
    	        		    continue;
    	        		}
    	        		else return false;
    	        	}
    	        	else{
    	        		if(n >= 192 || n < 128) return false;
    	        		len--;
    	        	}
    	        }
    	        return len == 0;
    	    }
    }
    

Log in to reply
 

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