From acc4ea927003eda8cb2c7037ebc816594156e32c Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 17 Nov 2025 07:42:03 +0800 Subject: [PATCH] feat: add rust solution to lc problem: No.1437 --- .../README.md | 25 ++++++++++++++--- .../README_EN.md | 27 ++++++++++++++++++- .../Solution.rs | 14 ++++++++++ 3 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 solution/1400-1499/1437.Check If All 1's Are at Least Length K Places Away/Solution.rs diff --git a/solution/1400-1499/1437.Check If All 1's Are at Least Length K Places Away/README.md b/solution/1400-1499/1437.Check If All 1's Are at Least Length K Places Away/README.md index 283ab683fde28..39df4efe7ce35 100644 --- a/solution/1400-1499/1437.Check If All 1's Are at Least Length K Places Away/README.md +++ b/solution/1400-1499/1437.Check If All 1's Are at Least Length K Places Away/README.md @@ -58,11 +58,11 @@ tags: ### 方法一:模拟 -我们可以遍历数组 $nums$,用变量 $j$ 记录上一个 $1$ 的下标,那么当前位置 $i$ 的元素为 $1$ 时,只需要判断 $i - j - 1$ 是否小于 $k$ 即可。如果小于 $k$,则说明存在两个 $1$ 之间的 $0$ 的个数小于 $k$,返回 `false`;否则,将 $j$ 更新为 $i$,继续遍历数组。 +我们可以遍历数组 $\textit{nums}$,用变量 $j$ 记录上一个 $1$ 的下标,那么当前位置 $i$ 的元素为 $1$ 时,只需要判断 $i - j - 1$ 是否小于 $k$ 即可。如果小于 $k$,则说明存在两个 $1$ 之间的 $0$ 的个数小于 $k$,返回 $\text{false}$;否则,将 $j$ 更新为 $i$,继续遍历数组。 -遍历结束后,返回 `true`。 +遍历结束后,返回 $\text{true}$。 -时间复杂度 $O(n)$,其中 $n$ 为数组 $nums$ 的长度。空间复杂度 $O(1)$。 +时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。 @@ -153,6 +153,25 @@ function kLengthApart(nums: number[], k: number): boolean { } ``` +#### Rust + +```rust +impl Solution { + pub fn k_length_apart(nums: Vec, k: i32) -> bool { + let mut j = -(k + 1); + for (i, &x) in nums.iter().enumerate() { + if x == 1 { + if (i as i32) - j - 1 < k { + return false; + } + j = i as i32; + } + } + true + } +} +``` + diff --git a/solution/1400-1499/1437.Check If All 1's Are at Least Length K Places Away/README_EN.md b/solution/1400-1499/1437.Check If All 1's Are at Least Length K Places Away/README_EN.md index 5feb700b3d2a1..5c1a667f0c4ef 100644 --- a/solution/1400-1499/1437.Check If All 1's Are at Least Length K Places Away/README_EN.md +++ b/solution/1400-1499/1437.Check If All 1's Are at Least Length K Places Away/README_EN.md @@ -52,7 +52,13 @@ tags: -### Solution 1 +### Solution 1: Simulation + +We can iterate through the array $\textit{nums}$ and use a variable $j$ to record the index of the previous $1$. When the element at the current position $i$ is $1$, we just need to check if $i - j - 1$ is less than $k$. If it is less than $k$, it means there exists a pair of $1$s with fewer than $k$ zeros between them, so we return $\text{false}$. Otherwise, we update $j$ to $i$ and continue iterating through the array. + +After the iteration is complete, we return $\text{true}$. + +The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$. @@ -143,6 +149,25 @@ function kLengthApart(nums: number[], k: number): boolean { } ``` +#### Rust + +```rust +impl Solution { + pub fn k_length_apart(nums: Vec, k: i32) -> bool { + let mut j = -(k + 1); + for (i, &x) in nums.iter().enumerate() { + if x == 1 { + if (i as i32) - j - 1 < k { + return false; + } + j = i as i32; + } + } + true + } +} +``` + diff --git a/solution/1400-1499/1437.Check If All 1's Are at Least Length K Places Away/Solution.rs b/solution/1400-1499/1437.Check If All 1's Are at Least Length K Places Away/Solution.rs new file mode 100644 index 0000000000000..585a24a32d128 --- /dev/null +++ b/solution/1400-1499/1437.Check If All 1's Are at Least Length K Places Away/Solution.rs @@ -0,0 +1,14 @@ +impl Solution { + pub fn k_length_apart(nums: Vec, k: i32) -> bool { + let mut j = -(k + 1); + for (i, &x) in nums.iter().enumerate() { + if x == 1 { + if (i as i32) - j - 1 < k { + return false; + } + j = i as i32; + } + } + true + } +}