Skip to content

Commit 694f9f7

Browse files
committed
2 parents 8fc9383 + a4bbf54 commit 694f9f7

4 files changed

Lines changed: 101 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
title: 717. 1-bit and 2-bit Characters
3+
description: We have two special characters:The first character can be represented by one bit 0. The second character can be represented by two bits (10 or 11). Given a binary array bits that ends with 0, return true if the last character must be a one-bit character.
4+
keywords: [LeetCode, 717. 1-bit and 2-bit Characters, Easy, Array]
5+
tags: [LeetCode, Easy, Array]
6+
---
7+
8+
# [{frontMatter.title}](https://leetcode.com/problems/1-bit-and-2-bit-characters/)
9+
10+
import Tabs from "@theme/Tabs";
11+
import TabItem from "@theme/TabItem";
12+
import CodeBlock from "@theme/CodeBlock";
13+
import DifficultyBadge from "@site/src/components/Badges/DifficultyBadge";
14+
import Solution from "!!raw-loader!./solution.js";
15+
import Description from "./_Description.md";
16+
import Examples from "./_Examples.md";
17+
18+
<DifficultyBadge difficulty="Easy" />
19+
20+
<Tabs>
21+
<TabItem value="description" label="題目描述" default>
22+
<details open>
23+
<summary>Description</summary>
24+
<Description />
25+
<Examples />
26+
</details>
27+
</TabItem>
28+
29+
<TabItem value="solution" label="解答">
30+
## Solution
31+
<CodeBlock language="js">{Solution}</CodeBlock>
32+
</TabItem>
33+
</Tabs>
34+
35+
## 解題思路
36+
37+
題目給一組只由 `0``1` 所組成的陣列,要判斷最後一位的 `0` 是否是單獨存在,而不是接續前面的 `[1,0]` 這樣的組合
38+
最後一位一定會是 `0`,所以只需要到 `bits.length - 1` 就行
39+
因為 return 時候會用到 `i` 來判斷結果,故不使用 for 選擇使用 while 迴圈來遍歷陣列
40+
41+
```js
42+
var isOneBitCharacter = function (bits) {
43+
let i = 0;
44+
while (i < bits.length - 1) {
45+
if (bits[i] === 0) {
46+
// 當前是 0 的話判斷下一個
47+
i++;
48+
} else {
49+
// 不是 0 就代表當前是 1 ,且 1 一定會組成 [1,0] 或 [1,1],所以 += 2
50+
i += 2;
51+
}
52+
}
53+
54+
return i === bits.length - 1;
55+
// 如果 i === bits.length - 1 的話就代表只剩下一個 0 ,反之則否
56+
};
57+
```
58+
59+
## 心得
60+
61+
題目描述超不直觀,花了一點時間才看懂
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
We have two special characters:
2+
3+
- The first character can be represented by one bit `0`.
4+
- The second character can be represented by two bits (`10` or `11`).
5+
6+
Given a binary array `bits` that ends with `0`, return `true` if the last character must be a one-bit character.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#### Example 1:
2+
3+
> **Input:** bits = [1,0,0]
4+
> **Output:** true
5+
> **Explanation:** The only way to decode it is two-bit character and one-bit character.
6+
> So the last character is one-bit character.
7+
8+
#### Example 2:
9+
10+
> **Input:** bits = [1,1,1,0]
11+
> **Output:** false
12+
> **Explanation:** he only way to decode it is two-bit character and two-bit character.
13+
> So the last character is not one-bit character.
14+
15+
## Constraints:
16+
17+
- `1 <= bits.length <= 1000`
18+
- `bits[i]` is either `0` or `1`.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number[]} bits
3+
* @return {boolean}
4+
*/
5+
var isOneBitCharacter = function (bits) {
6+
let i = 0;
7+
while (i < bits.length - 1) {
8+
if (bits[i] === 0) {
9+
i++;
10+
} else {
11+
i += 2;
12+
}
13+
}
14+
15+
return i === bits.length - 1;
16+
};

0 commit comments

Comments
 (0)