Skip to content

Commit 46325f9

Browse files
authored
feat: add rust solution to lc problem: No.1437 (#4843)
1 parent 4de1b27 commit 46325f9

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

solution/1400-1499/1437.Check If All 1's Are at Least Length K Places Away/README.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ tags:
5858

5959
### 方法一:模拟
6060

61-
我们可以遍历数组 $nums$,用变量 $j$ 记录上一个 $1$ 的下标,那么当前位置 $i$ 的元素为 $1$ 时,只需要判断 $i - j - 1$ 是否小于 $k$ 即可。如果小于 $k$,则说明存在两个 $1$ 之间的 $0$ 的个数小于 $k$,返回 `false`;否则,将 $j$ 更新为 $i$,继续遍历数组。
61+
我们可以遍历数组 $\textit{nums}$,用变量 $j$ 记录上一个 $1$ 的下标,那么当前位置 $i$ 的元素为 $1$ 时,只需要判断 $i - j - 1$ 是否小于 $k$ 即可。如果小于 $k$,则说明存在两个 $1$ 之间的 $0$ 的个数小于 $k$,返回 $\text{false}$;否则,将 $j$ 更新为 $i$,继续遍历数组。
6262

63-
遍历结束后,返回 `true`
63+
遍历结束后,返回 $\text{true}$
6464

65-
时间复杂度 $O(n)$,其中 $n$ 为数组 $nums$ 的长度。空间复杂度 $O(1)$。
65+
时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。
6666

6767
<!-- tabs:start -->
6868

@@ -153,6 +153,25 @@ function kLengthApart(nums: number[], k: number): boolean {
153153
}
154154
```
155155

156+
#### Rust
157+
158+
```rust
159+
impl Solution {
160+
pub fn k_length_apart(nums: Vec<i32>, k: i32) -> bool {
161+
let mut j = -(k + 1);
162+
for (i, &x) in nums.iter().enumerate() {
163+
if x == 1 {
164+
if (i as i32) - j - 1 < k {
165+
return false;
166+
}
167+
j = i as i32;
168+
}
169+
}
170+
true
171+
}
172+
}
173+
```
174+
156175
<!-- tabs:end -->
157176

158177
<!-- solution:end -->

solution/1400-1499/1437.Check If All 1's Are at Least Length K Places Away/README_EN.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ tags:
5252

5353
<!-- solution:start -->
5454

55-
### Solution 1
55+
### Solution 1: Simulation
56+
57+
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.
58+
59+
After the iteration is complete, we return $\text{true}$.
60+
61+
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.
5662

5763
<!-- tabs:start -->
5864

@@ -143,6 +149,25 @@ function kLengthApart(nums: number[], k: number): boolean {
143149
}
144150
```
145151

152+
#### Rust
153+
154+
```rust
155+
impl Solution {
156+
pub fn k_length_apart(nums: Vec<i32>, k: i32) -> bool {
157+
let mut j = -(k + 1);
158+
for (i, &x) in nums.iter().enumerate() {
159+
if x == 1 {
160+
if (i as i32) - j - 1 < k {
161+
return false;
162+
}
163+
j = i as i32;
164+
}
165+
}
166+
true
167+
}
168+
}
169+
```
170+
146171
<!-- tabs:end -->
147172

148173
<!-- solution:end -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
impl Solution {
2+
pub fn k_length_apart(nums: Vec<i32>, k: i32) -> bool {
3+
let mut j = -(k + 1);
4+
for (i, &x) in nums.iter().enumerate() {
5+
if x == 1 {
6+
if (i as i32) - j - 1 < k {
7+
return false;
8+
}
9+
j = i as i32;
10+
}
11+
}
12+
true
13+
}
14+
}

0 commit comments

Comments
 (0)