Skip to content

Commit 6684865

Browse files
committed
feat: add solutions to lc problem: No.0717
1 parent 65effac commit 6684865

File tree

6 files changed

+107
-9
lines changed

6 files changed

+107
-9
lines changed

solution/0700-0799/0717.1-bit and 2-bit Characters/README.md

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,16 @@ tags:
6060

6161
<!-- solution:start -->
6262

63-
### 方法一
63+
### 方法一:直接遍历
64+
65+
我们可以直接遍历数组 $\textit{bits}$ 的前 $n-1$ 个元素,每次根据当前元素的值决定跳过多少个元素:
66+
67+
- 如果当前元素为 $0$,则跳过 $1$ 个元素(表示一个一比特字符);
68+
- 如果当前元素为 $1$,则跳过 $2$ 个元素(表示一个两比特字符)。
69+
70+
当遍历结束时,如果当前索引等于 $n-1$,则说明最后一个字符是一个一比特字符,返回 $\text{true}$;否则,返回 $\text{false}$。
71+
72+
时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{bits}$ 的长度。空间复杂度 $O(1)$。
6473

6574
<!-- tabs:start -->
6675

@@ -96,7 +105,9 @@ class Solution {
96105
public:
97106
bool isOneBitCharacter(vector<int>& bits) {
98107
int i = 0, n = bits.size();
99-
while (i < n - 1) i += bits[i] + 1;
108+
while (i < n - 1) {
109+
i += bits[i] + 1;
110+
}
100111
return i == n - 1;
101112
}
102113
};
@@ -114,6 +125,34 @@ func isOneBitCharacter(bits []int) bool {
114125
}
115126
```
116127

128+
#### TypeScript
129+
130+
```ts
131+
function isOneBitCharacter(bits: number[]): boolean {
132+
let i = 0;
133+
const n = bits.length;
134+
while (i < n - 1) {
135+
i += bits[i] + 1;
136+
}
137+
return i === n - 1;
138+
}
139+
```
140+
141+
#### Rust
142+
143+
```rust
144+
impl Solution {
145+
pub fn is_one_bit_character(bits: Vec<i32>) -> bool {
146+
let mut i = 0usize;
147+
let n = bits.len();
148+
while i < n - 1 {
149+
i += (bits[i] + 1) as usize;
150+
}
151+
i == n - 1
152+
}
153+
}
154+
```
155+
117156
#### JavaScript
118157

119158
```js
@@ -127,7 +166,7 @@ var isOneBitCharacter = function (bits) {
127166
while (i < n - 1) {
128167
i += bits[i] + 1;
129168
}
130-
return i == n - 1;
169+
return i === n - 1;
131170
};
132171
```
133172

solution/0700-0799/0717.1-bit and 2-bit Characters/README_EN.md

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,16 @@ So the last character is not one-bit character.
5858

5959
<!-- solution:start -->
6060

61-
### Solution 1
61+
### Solution 1: Direct Traversal
62+
63+
We can directly traverse the first $n-1$ elements of the array $\textit{bits}$, and each time decide how many elements to skip based on the value of the current element:
64+
65+
- If the current element is $0$, skip $1$ element (representing a one-bit character);
66+
- If the current element is $1$, skip $2$ elements (representing a two-bit character).
67+
68+
When the traversal ends, if the current index equals $n-1$, it means the last character is a one-bit character, and we return $\text{true}$; otherwise, return $\text{false}$.
69+
70+
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{bits}$. The space complexity is $O(1)$.
6271

6372
<!-- tabs:start -->
6473

@@ -94,7 +103,9 @@ class Solution {
94103
public:
95104
bool isOneBitCharacter(vector<int>& bits) {
96105
int i = 0, n = bits.size();
97-
while (i < n - 1) i += bits[i] + 1;
106+
while (i < n - 1) {
107+
i += bits[i] + 1;
108+
}
98109
return i == n - 1;
99110
}
100111
};
@@ -112,6 +123,34 @@ func isOneBitCharacter(bits []int) bool {
112123
}
113124
```
114125

126+
#### TypeScript
127+
128+
```ts
129+
function isOneBitCharacter(bits: number[]): boolean {
130+
let i = 0;
131+
const n = bits.length;
132+
while (i < n - 1) {
133+
i += bits[i] + 1;
134+
}
135+
return i === n - 1;
136+
}
137+
```
138+
139+
#### Rust
140+
141+
```rust
142+
impl Solution {
143+
pub fn is_one_bit_character(bits: Vec<i32>) -> bool {
144+
let mut i = 0usize;
145+
let n = bits.len();
146+
while i < n - 1 {
147+
i += (bits[i] + 1) as usize;
148+
}
149+
i == n - 1
150+
}
151+
}
152+
```
153+
115154
#### JavaScript
116155

117156
```js
@@ -125,7 +164,7 @@ var isOneBitCharacter = function (bits) {
125164
while (i < n - 1) {
126165
i += bits[i] + 1;
127166
}
128-
return i == n - 1;
167+
return i === n - 1;
129168
};
130169
```
131170

solution/0700-0799/0717.1-bit and 2-bit Characters/Solution.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ class Solution {
22
public:
33
bool isOneBitCharacter(vector<int>& bits) {
44
int i = 0, n = bits.size();
5-
while (i < n - 1) i += bits[i] + 1;
5+
while (i < n - 1) {
6+
i += bits[i] + 1;
7+
}
68
return i == n - 1;
79
}
8-
};
10+
};

solution/0700-0799/0717.1-bit and 2-bit Characters/Solution.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ var isOneBitCharacter = function (bits) {
88
while (i < n - 1) {
99
i += bits[i] + 1;
1010
}
11-
return i == n - 1;
11+
return i === n - 1;
1212
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
impl Solution {
2+
pub fn is_one_bit_character(bits: Vec<i32>) -> bool {
3+
let mut i = 0usize;
4+
let n = bits.len();
5+
while i < n - 1 {
6+
i += (bits[i] + 1) as usize;
7+
}
8+
i == n - 1
9+
}
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function isOneBitCharacter(bits: number[]): boolean {
2+
let i = 0;
3+
const n = bits.length;
4+
while (i < n - 1) {
5+
i += bits[i] + 1;
6+
}
7+
return i === n - 1;
8+
}

0 commit comments

Comments
 (0)