LeetCode 题解工作台
UTF-8 编码验证
给定一个表示数据的整数数组 data ,返回它是否为有效的 UTF-8 编码。 UTF-8 中的一个字符可能的长度为 1 到 4 字节 ,遵循以下的规则: 对于 1 字节 的字符,字节的第一位设为 0 ,后面 7 位为这个符号的 unicode 码。 对于 n 字节 的字符 (n > 1),第一个字…
2
题型
5
代码语言
3
相关题
当前训练重点
中等 · 数组·结合·位运算·操作
答案摘要
我们用一个变量 记录当前需要填充的以 开头的字节的个数,初始时 $cnt = 0$。 遍历数组中的每个整数,对于每个整数 :
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 数组·结合·位运算·操作 题型思路
题目描述
给定一个表示数据的整数数组 data ,返回它是否为有效的 UTF-8 编码。
UTF-8 中的一个字符可能的长度为 1 到 4 字节,遵循以下的规则:
- 对于 1 字节 的字符,字节的第一位设为 0 ,后面 7 位为这个符号的 unicode 码。
- 对于 n 字节 的字符 (n > 1),第一个字节的前 n 位都设为1,第 n+1 位设为 0 ,后面字节的前两位一律设为 10 。剩下的没有提及的二进制位,全部为这个符号的 unicode 码。
这是 UTF-8 编码的工作方式:
Number of Bytes| UTF-8 octet sequence | (binary) --------------------+--------------------------------------------- 1 | 0xxxxxxx 2 | 110xxxxx 10xxxxxx 3 | 1110xxxx 10xxxxxx 10xxxxxx 4 | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
x 表示二进制形式的一位,可以是 0 或 1。
注意:输入是整数数组。只有每个整数的 最低 8 个有效位 用来存储数据。这意味着每个整数只表示 1 字节的数据。
示例 1:
输入:data = [197,130,1] 输出:true 解释:数据表示字节序列:11000101 10000010 00000001。 这是有效的 utf-8 编码,为一个 2 字节字符,跟着一个 1 字节字符。
示例 2:
输入:data = [235,140,4] 输出:false 解释:数据表示 8 位的序列: 11101011 10001100 00000100. 前 3 位都是 1 ,第 4 位为 0 表示它是一个 3 字节字符。 下一个字节是开头为 10 的延续字节,这是正确的。 但第二个延续字节不以 10 开头,所以是不符合规则的。
提示:
1 <= data.length <= 2 * 1040 <= data[i] <= 255
解题思路
方法一:一次遍历
我们用一个变量 记录当前需要填充的以 开头的字节的个数,初始时 。
遍历数组中的每个整数,对于每个整数 :
- 如果 ,则判断 是否以 开头,如果不是,则返回
false,否则 减一。 - 如果 的最高位为 ,则 。
- 如果 的最高两位为 ,则 。
- 如果 的最高三位为 ,则 。
- 如果 的最高四位为 ,则 。
- 否则,返回
false。
最后,如果 ,则返回 true,否则返回 false。
时间复杂度 ,其中 为数组 data 的长度。空间复杂度 。
class Solution:
def validUtf8(self, data: List[int]) -> bool:
cnt = 0
for v in data:
if cnt > 0:
if v >> 6 != 0b10:
return False
cnt -= 1
elif v >> 7 == 0:
cnt = 0
elif v >> 5 == 0b110:
cnt = 1
elif v >> 4 == 0b1110:
cnt = 2
elif v >> 3 == 0b11110:
cnt = 3
else:
return False
return cnt == 0
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | O(1) |
面试官常问的追问
外企场景- question_mark
Look for clear identification of how many continuation bytes follow each byte.
- question_mark
Assess understanding of bit manipulation and byte operations.
- question_mark
Ensure the solution handles edge cases like incomplete byte sequences.
常见陷阱
外企场景- error
Failing to properly identify the number of expected continuation bytes based on the first byte's leading bits.
- error
Misunderstanding the rules of valid continuation bytes, particularly the '10' pattern.
- error
Incorrectly processing arrays with invalid sequences, such as bytes that don’t follow the proper encoding pattern.
进阶变体
外企场景- arrow_right_alt
Modify the problem to validate UTF-16 or UTF-32 encoding.
- arrow_right_alt
Introduce a larger input size to assess the solution's efficiency under heavy constraints.
- arrow_right_alt
Ask for an optimization to minimize space usage further.