0%

leetCode-393:UTF-8 Validation

问题描述

给定一个整数数组,要求判断这个是不是utf-8字符。题目链接:**点我**

样例输入输出

输入:[197,130,1]

输出:true

输入:[235,140,4]

输出:false

问题解法

此题直接按照题意描述的规则进行判断即可,代码如下

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
class Solution {
private static final int ONE_BYTE_START = 0;

private static final int ONE_BYTE_END = 0b01111111;

private static final int TWO_BYTE_START = 0b11000000;

private static final int TWO_BYTE_END = 0b11011111;

private static final int THREE_BYTE_START = 0b11100000;

private static final int THREE_BYTE_END = 0b11101111;

private static final int FOUR_BYTE_START = 0b11110000;

private static final int FOUR_BYTE_END = 0b11110111;

private static final int FOLLOW_BYTE_START = 0b10000000;

private static final int FOLLOW_BYTE_END = 0b10111111;

public boolean validUtf8(int[] data) {
int i = 0;
while (i < data.length) {
int num = data[i];
if (isFourByte(num)) {
boolean ok = isFollowByte(data, i + 1) && isFollowByte(data, i + 2) && isFollowByte(data, i + 3);
if (!ok) {
return false;
}

i += 4;
continue;
}

if (isThreeByte(num)) {
boolean ok = isFollowByte(data, i + 1) && isFollowByte(data, i + 2);
if (!ok) {
return false;
}

i += 3;
continue;
}

if (isTwoByte(num)) {
boolean ok = isFollowByte(data, i + 1);
if (!ok) {
return false;
}

i += 2;
continue;
}

if (isOneByte(num)) {
i++;
continue;
}

return false;
}

return true;
}

private boolean isOneByte(int num) {
return num >= ONE_BYTE_START && num <= ONE_BYTE_END;
}

private boolean isTwoByte(int num) {
return num >= TWO_BYTE_START && num <= TWO_BYTE_END;
}

private boolean isThreeByte(int num) {
return num >= THREE_BYTE_START && num <= THREE_BYTE_END;
}

private boolean isFourByte(int num) {
return num >= FOUR_BYTE_START && num <= FOUR_BYTE_END;
}

private boolean isFollowByte(int[] data, int i) {
return i < data.length && data[i] >= FOLLOW_BYTE_START && data[i] <= FOLLOW_BYTE_END;
}
}