LeetCode – UTF-8 Validation (Java)

A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules:

For 1-byte character, the first bit is a 0, followed by its unicode code.
For n-bytes character, the first n-bits are all one's, the n+1 bit is 0, followed by n-1 bytes with most significant 2 bits being 10.
This is how the UTF-8 encoding would work:

   Char. number range  |        UTF-8 octet sequence
      (hexadecimal)    |              (binary)
   0000 0000-0000 007F | 0xxxxxxx
   0000 0080-0000 07FF | 110xxxxx 10xxxxxx
   0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
   0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx

Given an array of integers representing the data, return whether it is a valid utf-8 encoding.

Java Solution

public boolean validUtf8(int[] data) {
    int i=0;
    int count=0;
        int v = data[i];
            if((v&240)==240 && (v&248)==240){
            }else if(((v&224)==224) && (v&240)==224){
            }else if((v&192)==192 && (v&224)==192){
            }else if((v|127)==127){
                return false;
            if((v&128)==128 && (v&192)==128){
                return false;
    return count==0;
Category >> Algorithms  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";