Skip to content

feat: add solutions to lc problem: No.393 #3016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 84 additions & 39 deletions solution/0300-0399/0393.UTF-8 Validation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,22 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:一次遍历

我们用一个变量 $cnt$ 记录当前需要填充的以 $10$ 开头的字节的个数,初始时 $cnt = 0$。

遍历数组中的每个整数,对于每个整数 $v$:

- 如果 $cnt > 0$,则判断 $v$ 是否以 $10$ 开头,如果不是,则返回 `false`,否则 $cnt$ 减一。
- 如果 $v$ 的最高位为 $0$,则 $cnt = 0$。
- 如果 $v$ 的最高两位为 $110$,则 $cnt = 1$。
- 如果 $v$ 的最高三位为 $1110$,则 $cnt = 2$。
- 如果 $v$ 的最高四位为 $11110$,则 $cnt = 3$。
- 否则,返回 `false`。

最后,如果 $cnt = 0$,则返回 `true`,否则返回 `false`。

时间复杂度 $O(n)$,其中 $n$ 为数组 `data` 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -88,50 +103,50 @@ tags:
```python
class Solution:
def validUtf8(self, data: List[int]) -> bool:
n = 0
cnt = 0
for v in data:
if n > 0:
if cnt > 0:
if v >> 6 != 0b10:
return False
n -= 1
cnt -= 1
elif v >> 7 == 0:
n = 0
cnt = 0
elif v >> 5 == 0b110:
n = 1
cnt = 1
elif v >> 4 == 0b1110:
n = 2
cnt = 2
elif v >> 3 == 0b11110:
n = 3
cnt = 3
else:
return False
return n == 0
return cnt == 0
```

#### Java

```java
class Solution {
public boolean validUtf8(int[] data) {
int n = 0;
int cnt = 0;
for (int v : data) {
if (n > 0) {
if (cnt > 0) {
if (v >> 6 != 0b10) {
return false;
}
--n;
--cnt;
} else if (v >> 7 == 0) {
n = 0;
cnt = 0;
} else if (v >> 5 == 0b110) {
n = 1;
cnt = 1;
} else if (v >> 4 == 0b1110) {
n = 2;
cnt = 2;
} else if (v >> 3 == 0b11110) {
n = 3;
cnt = 3;
} else {
return false;
}
}
return n == 0;
return cnt == 0;
}
}
```
Expand All @@ -142,23 +157,26 @@ class Solution {
class Solution {
public:
bool validUtf8(vector<int>& data) {
int n = 0;
int cnt = 0;
for (int& v : data) {
if (n > 0) {
if (v >> 6 != 0b10) return false;
--n;
} else if (v >> 7 == 0)
n = 0;
else if (v >> 5 == 0b110)
n = 1;
else if (v >> 4 == 0b1110)
n = 2;
else if (v >> 3 == 0b11110)
n = 3;
else
if (cnt > 0) {
if (v >> 6 != 0b10) {
return false;
}
--cnt;
} else if (v >> 7 == 0) {
cnt = 0;
} else if (v >> 5 == 0b110) {
cnt = 1;
} else if (v >> 4 == 0b1110) {
cnt = 2;
} else if (v >> 3 == 0b11110) {
cnt = 3;
} else {
return false;
}
}
return n == 0;
return cnt == 0;
}
};
```
Expand All @@ -167,26 +185,53 @@ public:

```go
func validUtf8(data []int) bool {
n := 0
cnt := 0
for _, v := range data {
if n > 0 {
if cnt > 0 {
if v>>6 != 0b10 {
return false
}
n--
cnt--
} else if v>>7 == 0 {
n = 0
cnt = 0
} else if v>>5 == 0b110 {
n = 1
cnt = 1
} else if v>>4 == 0b1110 {
n = 2
cnt = 2
} else if v>>3 == 0b11110 {
n = 3
cnt = 3
} else {
return false
}
}
return n == 0
return cnt == 0
}
```

#### TypeScript

```ts
function validUtf8(data: number[]): boolean {
let cnt = 0;
for (const v of data) {
if (cnt > 0) {
if (v >> 6 != 0b10) {
return false;
}
--cnt;
} else if (v >> 7 == 0) {
cnt = 0;
} else if (v >> 5 == 0b110) {
cnt = 1;
} else if (v >> 4 == 0b1110) {
cnt = 2;
} else if (v >> 3 == 0b11110) {
cnt = 3;
} else {
return false;
}
}
return cnt == 0;
}
```

Expand Down
123 changes: 84 additions & 39 deletions solution/0300-0399/0393.UTF-8 Validation/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,22 @@ But the second continuation byte does not start with 10, so it is invalid.

<!-- solution:start -->

### Solution 1
### Solution 1: Single Pass

We use a variable $cnt$ to record the current number of bytes that need to be filled starting with $10$, initially $cnt = 0$.

For each integer $v$ in the array:

- If $cnt > 0$, then check if $v$ starts with $10$. If not, return `false`, otherwise decrement $cnt$.
- If the highest bit of $v$ is $0$, then $cnt = 0$.
- If the highest two bits of $v$ are $110$, then $cnt = 1$.
- If the highest three bits of $v$ are $1110$, then $cnt = 2$.
- If the highest four bits of $v$ are $11110$, then $cnt = 3$.
- Otherwise, return `false`.

Finally, if $cnt = 0$, return `true`, otherwise return `false`.

The time complexity is $O(n)$, where $n$ is the length of the array `data`. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand All @@ -86,50 +101,50 @@ But the second continuation byte does not start with 10, so it is invalid.
```python
class Solution:
def validUtf8(self, data: List[int]) -> bool:
n = 0
cnt = 0
for v in data:
if n > 0:
if cnt > 0:
if v >> 6 != 0b10:
return False
n -= 1
cnt -= 1
elif v >> 7 == 0:
n = 0
cnt = 0
elif v >> 5 == 0b110:
n = 1
cnt = 1
elif v >> 4 == 0b1110:
n = 2
cnt = 2
elif v >> 3 == 0b11110:
n = 3
cnt = 3
else:
return False
return n == 0
return cnt == 0
```

#### Java

```java
class Solution {
public boolean validUtf8(int[] data) {
int n = 0;
int cnt = 0;
for (int v : data) {
if (n > 0) {
if (cnt > 0) {
if (v >> 6 != 0b10) {
return false;
}
--n;
--cnt;
} else if (v >> 7 == 0) {
n = 0;
cnt = 0;
} else if (v >> 5 == 0b110) {
n = 1;
cnt = 1;
} else if (v >> 4 == 0b1110) {
n = 2;
cnt = 2;
} else if (v >> 3 == 0b11110) {
n = 3;
cnt = 3;
} else {
return false;
}
}
return n == 0;
return cnt == 0;
}
}
```
Expand All @@ -140,23 +155,26 @@ class Solution {
class Solution {
public:
bool validUtf8(vector<int>& data) {
int n = 0;
int cnt = 0;
for (int& v : data) {
if (n > 0) {
if (v >> 6 != 0b10) return false;
--n;
} else if (v >> 7 == 0)
n = 0;
else if (v >> 5 == 0b110)
n = 1;
else if (v >> 4 == 0b1110)
n = 2;
else if (v >> 3 == 0b11110)
n = 3;
else
if (cnt > 0) {
if (v >> 6 != 0b10) {
return false;
}
--cnt;
} else if (v >> 7 == 0) {
cnt = 0;
} else if (v >> 5 == 0b110) {
cnt = 1;
} else if (v >> 4 == 0b1110) {
cnt = 2;
} else if (v >> 3 == 0b11110) {
cnt = 3;
} else {
return false;
}
}
return n == 0;
return cnt == 0;
}
};
```
Expand All @@ -165,26 +183,53 @@ public:

```go
func validUtf8(data []int) bool {
n := 0
cnt := 0
for _, v := range data {
if n > 0 {
if cnt > 0 {
if v>>6 != 0b10 {
return false
}
n--
cnt--
} else if v>>7 == 0 {
n = 0
cnt = 0
} else if v>>5 == 0b110 {
n = 1
cnt = 1
} else if v>>4 == 0b1110 {
n = 2
cnt = 2
} else if v>>3 == 0b11110 {
n = 3
cnt = 3
} else {
return false
}
}
return n == 0
return cnt == 0
}
```

#### TypeScript

```ts
function validUtf8(data: number[]): boolean {
let cnt = 0;
for (const v of data) {
if (cnt > 0) {
if (v >> 6 != 0b10) {
return false;
}
--cnt;
} else if (v >> 7 == 0) {
cnt = 0;
} else if (v >> 5 == 0b110) {
cnt = 1;
} else if (v >> 4 == 0b1110) {
cnt = 2;
} else if (v >> 3 == 0b11110) {
cnt = 3;
} else {
return false;
}
}
return cnt == 0;
}
```

Expand Down
Loading
Loading