Skip to content

Commit 18a93fa

Browse files
authored
feat: add rust solutions to lc problem: No.2200 (#4520)
No.2200.Find All K-Distant Indices in an Array
1 parent 2bc4cdf commit 18a93fa

File tree

5 files changed

+228
-0
lines changed

5 files changed

+228
-0
lines changed

solution/2200-2299/2200.Find All K-Distant Indices in an Array/README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,26 @@ function findKDistantIndices(nums: number[], key: number, k: number): number[] {
170170
}
171171
```
172172

173+
#### Rust
174+
175+
```rust
176+
impl Solution {
177+
pub fn find_k_distant_indices(nums: Vec<i32>, key: i32, k: i32) -> Vec<i32> {
178+
let n = nums.len();
179+
let mut ans = Vec::new();
180+
for i in 0..n {
181+
for j in 0..n {
182+
if (i as i32 - j as i32).abs() <= k && nums[j] == key {
183+
ans.push(i as i32);
184+
break;
185+
}
186+
}
187+
}
188+
ans
189+
}
190+
}
191+
```
192+
173193
<!-- tabs:end -->
174194

175195
<!-- solution:end -->
@@ -309,6 +329,46 @@ function findKDistantIndices(nums: number[], key: number, k: number): number[] {
309329
}
310330
```
311331

332+
#### Rust
333+
334+
```rust
335+
impl Solution {
336+
pub fn find_k_distant_indices(nums: Vec<i32>, key: i32, k: i32) -> Vec<i32> {
337+
let n = nums.len();
338+
let mut idx = Vec::new();
339+
for i in 0..n {
340+
if nums[i] == key {
341+
idx.push(i as i32);
342+
}
343+
}
344+
345+
let search = |x: i32| -> usize {
346+
let (mut l, mut r) = (0, idx.len());
347+
while l < r {
348+
let mid = (l + r) >> 1;
349+
if idx[mid] >= x {
350+
r = mid;
351+
} else {
352+
l = mid + 1;
353+
}
354+
}
355+
l
356+
};
357+
358+
let mut ans = Vec::new();
359+
for i in 0..n {
360+
let l = search(i as i32 - k);
361+
let r = search(i as i32 + k + 1) as i32 - 1;
362+
if l as i32 <= r {
363+
ans.push(i as i32);
364+
}
365+
}
366+
367+
ans
368+
}
369+
}
370+
```
371+
312372
<!-- tabs:end -->
313373

314374
<!-- solution:end -->
@@ -414,6 +474,27 @@ function findKDistantIndices(nums: number[], key: number, k: number): number[] {
414474
}
415475
```
416476

477+
#### Rust
478+
479+
```rust
480+
impl Solution {
481+
pub fn find_k_distant_indices(nums: Vec<i32>, key: i32, k: i32) -> Vec<i32> {
482+
let n = nums.len();
483+
let mut ans = Vec::new();
484+
let mut j = 0;
485+
for i in 0..n {
486+
while j < i.saturating_sub(k as usize) || (j < n && nums[j] != key) {
487+
j += 1;
488+
}
489+
if j < n && j <= i + k as usize {
490+
ans.push(i as i32);
491+
}
492+
}
493+
ans
494+
}
495+
}
496+
```
497+
417498
<!-- tabs:end -->
418499

419500
<!-- solution:end -->

solution/2200-2299/2200.Find All K-Distant Indices in an Array/README_EN.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,26 @@ function findKDistantIndices(nums: number[], key: number, k: number): number[] {
168168
}
169169
```
170170

171+
#### Rust
172+
173+
```rust
174+
impl Solution {
175+
pub fn find_k_distant_indices(nums: Vec<i32>, key: i32, k: i32) -> Vec<i32> {
176+
let n = nums.len();
177+
let mut ans = Vec::new();
178+
for i in 0..n {
179+
for j in 0..n {
180+
if (i as i32 - j as i32).abs() <= k && nums[j] == key {
181+
ans.push(i as i32);
182+
break;
183+
}
184+
}
185+
}
186+
ans
187+
}
188+
}
189+
```
190+
171191
<!-- tabs:end -->
172192

173193
<!-- solution:end -->
@@ -307,6 +327,46 @@ function findKDistantIndices(nums: number[], key: number, k: number): number[] {
307327
}
308328
```
309329

330+
#### Rust
331+
332+
```rust
333+
impl Solution {
334+
pub fn find_k_distant_indices(nums: Vec<i32>, key: i32, k: i32) -> Vec<i32> {
335+
let n = nums.len();
336+
let mut idx = Vec::new();
337+
for i in 0..n {
338+
if nums[i] == key {
339+
idx.push(i as i32);
340+
}
341+
}
342+
343+
let search = |x: i32| -> usize {
344+
let (mut l, mut r) = (0, idx.len());
345+
while l < r {
346+
let mid = (l + r) >> 1;
347+
if idx[mid] >= x {
348+
r = mid;
349+
} else {
350+
l = mid + 1;
351+
}
352+
}
353+
l
354+
};
355+
356+
let mut ans = Vec::new();
357+
for i in 0..n {
358+
let l = search(i as i32 - k);
359+
let r = search(i as i32 + k + 1) as i32 - 1;
360+
if l as i32 <= r {
361+
ans.push(i as i32);
362+
}
363+
}
364+
365+
ans
366+
}
367+
}
368+
```
369+
310370
<!-- tabs:end -->
311371

312372
<!-- solution:end -->
@@ -412,6 +472,27 @@ function findKDistantIndices(nums: number[], key: number, k: number): number[] {
412472
}
413473
```
414474

475+
#### Rust
476+
477+
```rust
478+
impl Solution {
479+
pub fn find_k_distant_indices(nums: Vec<i32>, key: i32, k: i32) -> Vec<i32> {
480+
let n = nums.len();
481+
let mut ans = Vec::new();
482+
let mut j = 0;
483+
for i in 0..n {
484+
while j < i.saturating_sub(k as usize) || (j < n && nums[j] != key) {
485+
j += 1;
486+
}
487+
if j < n && j <= i + k as usize {
488+
ans.push(i as i32);
489+
}
490+
}
491+
ans
492+
}
493+
}
494+
```
495+
415496
<!-- tabs:end -->
416497

417498
<!-- solution:end -->
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn find_k_distant_indices(nums: Vec<i32>, key: i32, k: i32) -> Vec<i32> {
3+
let n = nums.len();
4+
let mut ans = Vec::new();
5+
for i in 0..n {
6+
for j in 0..n {
7+
if (i as i32 - j as i32).abs() <= k && nums[j] == key {
8+
ans.push(i as i32);
9+
break;
10+
}
11+
}
12+
}
13+
ans
14+
}
15+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
impl Solution {
2+
pub fn find_k_distant_indices(nums: Vec<i32>, key: i32, k: i32) -> Vec<i32> {
3+
let n = nums.len();
4+
let mut idx = Vec::new();
5+
for i in 0..n {
6+
if nums[i] == key {
7+
idx.push(i as i32);
8+
}
9+
}
10+
11+
let search = |x: i32| -> usize {
12+
let (mut l, mut r) = (0, idx.len());
13+
while l < r {
14+
let mid = (l + r) >> 1;
15+
if idx[mid] >= x {
16+
r = mid;
17+
} else {
18+
l = mid + 1;
19+
}
20+
}
21+
l
22+
};
23+
24+
let mut ans = Vec::new();
25+
for i in 0..n {
26+
let l = search(i as i32 - k);
27+
let r = search(i as i32 + k + 1) as i32 - 1;
28+
if l as i32 <= r {
29+
ans.push(i as i32);
30+
}
31+
}
32+
33+
ans
34+
}
35+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn find_k_distant_indices(nums: Vec<i32>, key: i32, k: i32) -> Vec<i32> {
3+
let n = nums.len();
4+
let mut ans = Vec::new();
5+
let mut j = 0;
6+
for i in 0..n {
7+
while j < i.saturating_sub(k as usize) || (j < n && nums[j] != key) {
8+
j += 1;
9+
}
10+
if j < n && j <= i + k as usize {
11+
ans.push(i as i32);
12+
}
13+
}
14+
ans
15+
}
16+
}

0 commit comments

Comments
 (0)