diff --git a/solution/0700-0799/0717.1-bit and 2-bit Characters/README.md b/solution/0700-0799/0717.1-bit and 2-bit Characters/README.md index 0f600671cf068..3b04af3d62115 100644 --- a/solution/0700-0799/0717.1-bit and 2-bit Characters/README.md +++ b/solution/0700-0799/0717.1-bit and 2-bit Characters/README.md @@ -60,7 +60,16 @@ tags: -### 方法一 +### 方法一:直接遍历 + +我们可以直接遍历数组 $\textit{bits}$ 的前 $n-1$ 个元素,每次根据当前元素的值决定跳过多少个元素: + +- 如果当前元素为 $0$,则跳过 $1$ 个元素(表示一个一比特字符); +- 如果当前元素为 $1$,则跳过 $2$ 个元素(表示一个两比特字符)。 + +当遍历结束时,如果当前索引等于 $n-1$,则说明最后一个字符是一个一比特字符,返回 $\text{true}$;否则,返回 $\text{false}$。 + +时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{bits}$ 的长度。空间复杂度 $O(1)$。 @@ -96,7 +105,9 @@ class Solution { public: bool isOneBitCharacter(vector& 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; } }; @@ -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) -> bool { + let mut i = 0usize; + let n = bits.len(); + while i < n - 1 { + i += (bits[i] + 1) as usize; + } + i == n - 1 + } +} +``` + #### JavaScript ```js @@ -127,7 +166,7 @@ var isOneBitCharacter = function (bits) { while (i < n - 1) { i += bits[i] + 1; } - return i == n - 1; + return i === n - 1; }; ``` diff --git a/solution/0700-0799/0717.1-bit and 2-bit Characters/README_EN.md b/solution/0700-0799/0717.1-bit and 2-bit Characters/README_EN.md index 6410e3ded0e5b..8decda7283f42 100644 --- a/solution/0700-0799/0717.1-bit and 2-bit Characters/README_EN.md +++ b/solution/0700-0799/0717.1-bit and 2-bit Characters/README_EN.md @@ -58,7 +58,16 @@ So the last character is not one-bit character. -### 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)$. @@ -94,7 +103,9 @@ class Solution { public: bool isOneBitCharacter(vector& 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; } }; @@ -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) -> bool { + let mut i = 0usize; + let n = bits.len(); + while i < n - 1 { + i += (bits[i] + 1) as usize; + } + i == n - 1 + } +} +``` + #### JavaScript ```js @@ -125,7 +164,7 @@ var isOneBitCharacter = function (bits) { while (i < n - 1) { i += bits[i] + 1; } - return i == n - 1; + return i === n - 1; }; ``` diff --git a/solution/0700-0799/0717.1-bit and 2-bit Characters/Solution.cpp b/solution/0700-0799/0717.1-bit and 2-bit Characters/Solution.cpp index a2bed6c314d0e..3df9a0a4cd14c 100644 --- a/solution/0700-0799/0717.1-bit and 2-bit Characters/Solution.cpp +++ b/solution/0700-0799/0717.1-bit and 2-bit Characters/Solution.cpp @@ -2,7 +2,9 @@ class Solution { public: bool isOneBitCharacter(vector& 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; } -}; \ No newline at end of file +}; diff --git a/solution/0700-0799/0717.1-bit and 2-bit Characters/Solution.js b/solution/0700-0799/0717.1-bit and 2-bit Characters/Solution.js index 54f66d34d68f7..82adc533614ec 100644 --- a/solution/0700-0799/0717.1-bit and 2-bit Characters/Solution.js +++ b/solution/0700-0799/0717.1-bit and 2-bit Characters/Solution.js @@ -8,5 +8,5 @@ var isOneBitCharacter = function (bits) { while (i < n - 1) { i += bits[i] + 1; } - return i == n - 1; + return i === n - 1; }; diff --git a/solution/0700-0799/0717.1-bit and 2-bit Characters/Solution.rs b/solution/0700-0799/0717.1-bit and 2-bit Characters/Solution.rs new file mode 100644 index 0000000000000..fbdfc3b385ee6 --- /dev/null +++ b/solution/0700-0799/0717.1-bit and 2-bit Characters/Solution.rs @@ -0,0 +1,10 @@ +impl Solution { + pub fn is_one_bit_character(bits: Vec) -> bool { + let mut i = 0usize; + let n = bits.len(); + while i < n - 1 { + i += (bits[i] + 1) as usize; + } + i == n - 1 + } +} diff --git a/solution/0700-0799/0717.1-bit and 2-bit Characters/Solution.ts b/solution/0700-0799/0717.1-bit and 2-bit Characters/Solution.ts new file mode 100644 index 0000000000000..14051bc185d66 --- /dev/null +++ b/solution/0700-0799/0717.1-bit and 2-bit Characters/Solution.ts @@ -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; +}