Java simple concise solution


  • 0
    W
    1. If data[i] >= 128 && data[i] < 192 return false
    2. if data[i] >= 192, it means it is a n-bytes character, we should count the first N bits.
    3. Check if current position + N is out of the boundary
    4. Check if the next N - 1 bytes are started with 10 which means larger than or equal to 128 and smaller than 192
    5. We don't need to care about 1-byte character
      public boolean validUtf8(int[] data) {
            for (int i = 0; i < data.length; i++) {
                if (data[i] >= 128 && data[i] < 192) return false;
                if (data[i] >= 192) {
                    int mask = 32, cnt = 2;
                    while (mask >= 0 && (mask & data[i]) != 0) {
                        if (++cnt + i > data.length) return false;
                        mask = mask >> 1;
                    }
                    int j = i + 1;
                    for ( ; j < i + cnt; j++) {
                        if (data[j] < 128 || data[j] >= 192) return false;
                    }
                    i = j - 1;
                }
            }
            return true;
        }

Log in to reply
 

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