Skip to content

Commit 22b8c60

Browse files
committed
feat: add the solution of 1-bit and 2-bit Characters(717) with Java.
1 parent 4c65ff1 commit 22b8c60

File tree

3 files changed

+62
-20
lines changed

3 files changed

+62
-20
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
| [695][695-question] | [Max Area of Island][695-tips] | [][695-java] | [][695-js] | |
8080
| [696][696-question] | [Count Binary Substrings][696-tips] | [][696-java] | [][696-js] | |
8181
| [697][697-question] | [Degree of an Array][697-tips] | [][697-java] | [][697-js] | |
82-
| [717][717-question] | [1-bit and 2-bit Characters][717-tips] | | [][717-js] | |
82+
| [717][717-question] | [1-bit and 2-bit Characters][717-tips] | [][717-java] | [][717-js] | |
8383
| [720][720-question] | [Longest Word in Dictionary][720-tips] | | [][720-js] | |
8484
| [724][724-question] | [Find Pivot Index][724-tips] | | [][724-js] | |
8585
| [728][728-question] | [Self Dividing Numbers][728-tips] | [][728-java] | [][728-js] | |
@@ -477,6 +477,7 @@
477477
[695-java]: ./src/_695/Solution.java
478478
[696-java]: ./src/_696/Solution.java
479479
[697-java]: ./src/_697/Solution.java
480+
[717-java]: ./src/_717/Solution.java
480481
[728-java]: ./src/_728/Solution.java
481482
[771-java]: ./src/_771/Solution.java
482483
[804-java]: ./src/_804/Solution.java

src/_717/Solution.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package _717;
2+
3+
public class Solution {
4+
public boolean isOneBitCharacter(int[] bits) {
5+
int i = bits.length - 2;
6+
while (i >= 0 && bits[i] != 0) i--;
7+
return ((bits.length - i) & 1) == 0;
8+
}
9+
10+
public static void main(String[] args) {
11+
Solution solution = new Solution();
12+
System.out.println(solution.isOneBitCharacter(new int[]{1, 1, 0}));
13+
System.out.println(solution.isOneBitCharacter(new int[]{1, 1, 1, 0}));
14+
}
15+
}

tips/717/README.md

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,72 @@
1-
[xxxx][title]
1+
[1-bit and 2-bit Characters][title]
22

33
## Description
4-
// 抄题目
54

5+
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`).
66

7-
**Example:**
7+
Now given a string represented by several bits. Return whether the last character must be a one-bit character or not. The given string will always end with a zero.
8+
9+
**Example 1:**
810

911
```
10-
// 抄Example
12+
Input:
13+
bits = [1, 0, 0]
14+
Output: True
15+
Explanation:
16+
The only way to decode it is two-bit character and one-bit character. So the last character is one-bit character.
17+
```
18+
19+
**Example 2:**
20+
21+
```
22+
Input:
23+
bits = [1, 1, 1, 0]
24+
Output: False
25+
Explanation:
26+
The only way to decode it is two-bit character and two-bit character. So the last character is NOT one-bit character.
1127
```
1228

1329
**Note:**
14-
// Note
1530

16-
**Tags:** // tags
31+
`1 <= len(bits) <= 1000`.
32+
33+
`bits[i]` is always `0` or `1`.
1734

35+
**Tags:** [Array](https://leetcode.com/tag/array/)
1836

1937
## 思路 1
20-
// 贴一些关键代码,说一些解题思路
21-
// (同一种语言可以写多种思路,与某种语言思路相同的另一种语言的思路无须赘述,但可以把代码贴在后面)
22-
```java
2338

24-
```
25-
```javascript
39+
观察可以发现,其实每个编码数组都只有一种解码方式,可以直接从头开始解码。
40+
41+
**Java:**
2642

43+
```java
44+
public boolean isOneBitCharacter(int[] bits) {
45+
int i = 0;
46+
while (i < bits.length - 1) {
47+
i += bits[i] + 1;
48+
}
49+
return i == bits.length - 1;
50+
}
2751
```
2852

2953
## 思路 2
30-
// 贴一些关键代码,说一些解题思路
31-
```java
3254

33-
```
55+
再观察我们可以发现,`0`肯定是一个字符编码的结尾。且题目指出所给的编码数组的最后一个编码肯定为`0`,那么我们只要找到倒数第二个`0`的位置即可,因为这两个`0`之间的`1`的个数为偶数个时,当且仅当最后一个字符的编码为一个单独的`0`
3456

35-
## 思路 3
36-
// 贴一些关键代码,说一些解题思路
37-
```kotlin
57+
**Java:**
3858

59+
```java
60+
public boolean isOneBitCharacter(int[] bits) {
61+
int i = bits.length - 2;
62+
while (i >= 0 && bits[i] != 0) i--;
63+
return ((bits.length - i) & 1) == 0;
64+
}
3965
```
4066

4167
## 结语
42-
68+
4369
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]
4470

45-
[title]: https://leetcode.com/problems/xxxx
71+
[title]: https://leetcode.com/problems/1-bit-and-2-bit-characters/description/
4672
[ls]: https://github.com/RichCodersAndMe/LeetCode-Solution

0 commit comments

Comments
 (0)