Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
45 changes: 42 additions & 3 deletions solution/0700-0799/0717.1-bit and 2-bit Characters/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,16 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:直接遍历

我们可以直接遍历数组 $\textit{bits}$ 的前 $n-1$ 个元素,每次根据当前元素的值决定跳过多少个元素:

- 如果当前元素为 $0$,则跳过 $1$ 个元素(表示一个一比特字符);
- 如果当前元素为 $1$,则跳过 $2$ 个元素(表示一个两比特字符)。

当遍历结束时,如果当前索引等于 $n-1$,则说明最后一个字符是一个一比特字符,返回 $\text{true}$;否则,返回 $\text{false}$。

时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{bits}$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -96,7 +105,9 @@ class Solution {
public:
bool isOneBitCharacter(vector<int>& bits) {
int i = 0, n = bits.size();
while (i < n - 1) i += bits[i] + 1;
while (i < n - 1) {
i += bits[i] + 1;
}
return i == n - 1;
}
};
Expand All @@ -114,6 +125,34 @@ func isOneBitCharacter(bits []int) bool {
}
```

#### TypeScript

```ts
function isOneBitCharacter(bits: number[]): boolean {
let i = 0;
const n = bits.length;
while (i < n - 1) {
i += bits[i] + 1;
}
return i === n - 1;
}
```

#### Rust

```rust
impl Solution {
pub fn is_one_bit_character(bits: Vec<i32>) -> bool {
let mut i = 0usize;
let n = bits.len();
while i < n - 1 {
i += (bits[i] + 1) as usize;
}
i == n - 1
}
}
```

#### JavaScript

```js
Expand All @@ -127,7 +166,7 @@ var isOneBitCharacter = function (bits) {
while (i < n - 1) {
i += bits[i] + 1;
}
return i == n - 1;
return i === n - 1;
};
```

Expand Down
45 changes: 42 additions & 3 deletions solution/0700-0799/0717.1-bit and 2-bit Characters/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,16 @@ So the last character is not one-bit character.

<!-- solution:start -->

### Solution 1
### Solution 1: Direct Traversal

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:

- If the current element is $0$, skip $1$ element (representing a one-bit character);
- If the current element is $1$, skip $2$ elements (representing a two-bit character).

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}$.

The time complexity is $O(n)$, where $n$ is the length of the array $\textit{bits}$. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -94,7 +103,9 @@ class Solution {
public:
bool isOneBitCharacter(vector<int>& bits) {
int i = 0, n = bits.size();
while (i < n - 1) i += bits[i] + 1;
while (i < n - 1) {
i += bits[i] + 1;
}
return i == n - 1;
}
};
Expand All @@ -112,6 +123,34 @@ func isOneBitCharacter(bits []int) bool {
}
```

#### TypeScript

```ts
function isOneBitCharacter(bits: number[]): boolean {
let i = 0;
const n = bits.length;
while (i < n - 1) {
i += bits[i] + 1;
}
return i === n - 1;
}
```

#### Rust

```rust
impl Solution {
pub fn is_one_bit_character(bits: Vec<i32>) -> bool {
let mut i = 0usize;
let n = bits.len();
while i < n - 1 {
i += (bits[i] + 1) as usize;
}
i == n - 1
}
}
```

#### JavaScript

```js
Expand All @@ -125,7 +164,7 @@ var isOneBitCharacter = function (bits) {
while (i < n - 1) {
i += bits[i] + 1;
}
return i == n - 1;
return i === n - 1;
};
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ class Solution {
public:
bool isOneBitCharacter(vector<int>& bits) {
int i = 0, n = bits.size();
while (i < n - 1) i += bits[i] + 1;
while (i < n - 1) {
i += bits[i] + 1;
}
return i == n - 1;
}
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ var isOneBitCharacter = function (bits) {
while (i < n - 1) {
i += bits[i] + 1;
}
return i == n - 1;
return i === n - 1;
};
10 changes: 10 additions & 0 deletions solution/0700-0799/0717.1-bit and 2-bit Characters/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
impl Solution {
pub fn is_one_bit_character(bits: Vec<i32>) -> bool {
let mut i = 0usize;
let n = bits.len();
while i < n - 1 {
i += (bits[i] + 1) as usize;
}
i == n - 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function isOneBitCharacter(bits: number[]): boolean {
let i = 0;
const n = bits.length;
while (i < n - 1) {
i += bits[i] + 1;
}
return i === n - 1;
}