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

  1. For 1-byte character, the first bit is a 0, followed by its unicode code.
  2. 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:

1
2
3
4
5
6
7
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.

Note:
The input is an array of integers. Only the least significant 8 bits of each integer is used to store the data. This means each integer represents only 1 byte of data.

Example 1:

1
2
3
4
data = [197, 130, 1], which represents the octet sequence: 11000101 10000010 00000001.
Return true.
It is a valid utf-8 encoding for a 2-bytes character followed by a 1-byte character.

Example 2:

1
2
3
4
5
6
data = [235, 140, 4], which represented the octet sequence: 11101011 10001100 00000100.
Return false.
The first 3 bits are all one's and the 4th bit is 0 means it is a 3-bytes character.
The next byte is a continuation byte which starts with 10 and that's correct.
But the second continuation byte does not start with 10, so it is invalid.


我的做法很直接:

  1. 如果第一位是0,跳过,看下一个byte.
  2. 如果不是0,计算第一个0前面有几个1,也就是求n
  3. n 在 [2, 4] 之内,出了这个范围就不对了
  4. 检查后面n - 1个bytes. 看是否符合条件。

我的做法没什么取巧的地方,是一种很直接的思路。做题时各种数位数也是头疼。
代码中提供了两种计算n的方式,可惜没有O(1)的方案。调用Integer.numberOfLeadingZeros(b), 这个的时间复杂度是O(logn),略快。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public boolean validUtf8(int[] data) {
for (int i = 0; i < data.length; ) {
if ((data[i] & 0x80) == 0) {
i++;
continue;
}
int b = ~(data[i] | 0xffffff00);
int lz = Integer.numberOfLeadingZeros(b);
int n = 8 - (32 - lz);
// int n = 1;
// while(b < Math.pow(2, 8 - n - 1)) {
// n++;
// }
if (n <= 1 || n > 4)
return false;
for (int j = i + 1; j < i + n; j++) {
if (j >= data.length)
return false;
if ((data[j] & 0xc0) != 0x80) {
return false;
}
}
i = i + n;
}
return true;
}

看到一种很暴力的做法:
https://discuss.leetcode.com/topic/57192/one-pass-simple-solution

原程序中是用的数字而不是16进制表示,看着费劲,所以给改了一下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public boolean ValidUtf8(int[] data) {
int bitCount = 0;
for (int n : data) {
if (n >= 0xc0) { //1100 0000
if (bitCount != 0)
return false;
else if (n >= 0xf0) //1111 0000
bitCount = 3;
else if (n >= 0xe0) //1110 0000
bitCount = 2;
else
bitCount = 1;
} else if (n >= 0x80) { //1000 0000
bitCount--;
if (bitCount < 0)
return false;
} else if (bitCount > 0) {
return false;
}
}
return bitCount == 0;
}