Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dceaa1b

Browse files
✨feat: Add 393
1 parent 8b65a0e commit dceaa1b

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
| [345. 反转字符串中的元音字母](https://leetcode-cn.com/problems/reverse-vowels-of-a-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reverse-vowels-of-a-string/solution/gong-shui-san-xie-note-bie-pian-shuang-z-c8ii/) | 简单 | 🤩🤩🤩 |
4040
| [382. 链表随机节点](https://leetcode-cn.com/problems/linked-list-random-node/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/linked-list-random-node/solution/gong-shui-san-xie-xu-shui-chi-chou-yang-1lp9d/) | 中等 | 🤩🤩🤩🤩🤩 |
4141
| [383. 赎金信](https://leetcode-cn.com/problems/ransom-note/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/ransom-note/solution/gong-shui-san-xie-jian-dan-ji-shu-mo-ni-udpzn/) | 简单 | 🤩🤩🤩🤩🤩 |
42+
| [393. UTF-8 编码验证](https://leetcode-cn.com/problems/utf-8-validation/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/utf-8-validation/solution/by-ac_oier-zvoy/) | 中等 | 🤩🤩🤩🤩🤩 |
4243
| [400. 第 N 位数字](https://leetcode-cn.com/problems/nth-digit/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/nth-digit/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-w5wl/) | 中等 | 🤩🤩🤩🤩 |
4344
| [405. 数字转换为十六进制数](https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/solution/gong-shui-san-xie-yi-ti-shuang-jie-jin-z-d93o/) | 简单 | 🤩🤩🤩🤩 |
4445
| [412. Fizz Buzz](https://leetcode-cn.com/problems/fizz-buzz/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/fizz-buzz/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-jll0/) | 简单 | 🤩🤩🤩🤩 |
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[393. UTF-8 编码验证](https://leetcode-cn.com/problems/utf-8-validation/solution/by-ac_oier-zvoy/)** ,难度为 **中等**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
给定一个表示数据的整数数组 `data` ,返回它是否为有效的 $UTF-8$ 编码。
10+
11+
`UTF-8` 中的一个字符可能的长度为 1ドル$ 到 4ドル$ 字节,遵循以下的规则:
12+
13+
1. 对于 1ドル$ 字节 的字符,字节的第一位设为 0ドル$ ,后面 7ドル$ 位为这个符号的 `unicode` 码。
14+
2. 对于 $n$ 字节 的字符 ($n > 1$),第一个字节的前 $n$ 位都设为 1ドル,ドル第 $n+1$ 位设为 0ドル$ ,后面字节的前两位一律设为 10ドル$ 。剩下的没有提及的二进制位,全部为这个符号的 `unicode` 码。
15+
16+
这是 UTF-8 编码的工作方式:
17+
```
18+
Char. number range | UTF-8 octet sequence
19+
(hexadecimal) | (binary)
20+
--------------------+---------------------------------------------
21+
0000 0000-0000 007F | 0xxxxxxx
22+
0000 0080-0000 07FF | 110xxxxx 10xxxxxx
23+
0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
24+
0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
25+
```
26+
注意:输入是整数数组。只有每个整数的 最低 8 个有效位 用来存储数据。这意味着每个整数只表示 1 字节的数据。
27+
28+
29+
30+
示例 1:
31+
```
32+
输入:data = [197,130,1]
33+
34+
输出:true
35+
36+
解释:数据表示字节序列:11000101 10000010 00000001。
37+
这是有效的 utf-8 编码,为一个 2 字节字符,跟着一个 1 字节字符。
38+
```
39+
示例 2:
40+
```
41+
输入:data = [235,140,4]
42+
43+
输出:false
44+
45+
解释:数据表示 8 位的序列: 11101011 10001100 00000100.
46+
前 3 位都是 1 ,第 4 位为 0 表示它是一个 3 字节字符。
47+
下一个字节是开头为 10 的延续字节,这是正确的。
48+
但第二个延续字节不以 10 开头,所以是不符合规则的。
49+
```
50+
51+
提示:
52+
* 1ドル <= data.length <= 2 * 10^4$
53+
* 0ドル <= data[i] <= 255$
54+
55+
---
56+
57+
### 模拟
58+
59+
根据题意进行模拟即可。
60+
61+
从前往后处理每个 $data[i],ドル先统计 $data[i]$ 从第 7ドル$ 位开始往后有多少位连续的 1ドル,ドル代表这是一个几字节的字符,记为 $cnt$ :
62+
63+
* 如果 $cnt$ 为 1ドル$ 或者大于 4ドル$ 均违反编码规则(与字符长度为 1ドル$ 时的编码规则 和 字符长度只能是 1ドル$ 到 4ドル$ 冲突),返回 `False`;
64+
* 如果位置 $i$ 后面不足 $cnt - 1$ 也返回 `False`;
65+
* 否则检查下标范围为 $[i + 1, i + cnt - 1]$ 的数是否满足前两位为 10ドル$ 的要求,若不满足返回 `False`
66+
67+
如果上述过程满足要求,跳到下一个检查点进行检查,整个 `data` 都没有冲突则返回 `True`
68+
69+
代码:
70+
```Java
71+
class Solution {
72+
public boolean validUtf8(int[] data) {
73+
int n = data.length;
74+
for (int i = 0; i < n; ) {
75+
int t = data[i], j = 7;
76+
while (j >= 0 && (((t >> j) & 1) == 1)) j--;
77+
int cnt = 7 - j;
78+
if (cnt == 1 || cnt > 4) return false;
79+
if (i + cnt - 1 >= n) return false;
80+
for (int k = i + 1; k < i + cnt; k++) {
81+
if ((((data[k] >> 7) & 1) == 1) && (((data[k] >> 6) & 1) == 0)) continue;
82+
return false;
83+
}
84+
if (cnt == 0) i++;
85+
else i += cnt;
86+
}
87+
return true;
88+
}
89+
}
90+
```
91+
* 时间复杂度:$O(n)$
92+
* 空间复杂度:$O(1)$
93+
94+
---
95+
96+
### 最后
97+
98+
这是我们「刷穿 LeetCode」系列文章的第 `No.393` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
99+
100+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
101+
102+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
103+
104+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
105+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /