diff --git a/solution/2200-2299/2200.Find All K-Distant Indices in an Array/README.md b/solution/2200-2299/2200.Find All K-Distant Indices in an Array/README.md index d0f0cc6921a97..3c25fc4ddbce7 100644 --- a/solution/2200-2299/2200.Find All K-Distant Indices in an Array/README.md +++ b/solution/2200-2299/2200.Find All K-Distant Indices in an Array/README.md @@ -170,6 +170,26 @@ function findKDistantIndices(nums: number[], key: number, k: number): number[] { } ``` +#### Rust + +```rust +impl Solution { + pub fn find_k_distant_indices(nums: Vec, key: i32, k: i32) -> Vec { + let n = nums.len(); + let mut ans = Vec::new(); + for i in 0..n { + for j in 0..n { + if (i as i32 - j as i32).abs() <= k && nums[j] == key { + ans.push(i as i32); + break; + } + } + } + ans + } +} +``` + @@ -309,6 +329,46 @@ function findKDistantIndices(nums: number[], key: number, k: number): number[] { } ``` +#### Rust + +```rust +impl Solution { + pub fn find_k_distant_indices(nums: Vec, key: i32, k: i32) -> Vec { + let n = nums.len(); + let mut idx = Vec::new(); + for i in 0..n { + if nums[i] == key { + idx.push(i as i32); + } + } + + let search = |x: i32| -> usize { + let (mut l, mut r) = (0, idx.len()); + while l < r { + let mid = (l + r) >> 1; + if idx[mid] >= x { + r = mid; + } else { + l = mid + 1; + } + } + l + }; + + let mut ans = Vec::new(); + for i in 0..n { + let l = search(i as i32 - k); + let r = search(i as i32 + k + 1) as i32 - 1; + if l as i32 <= r { + ans.push(i as i32); + } + } + + ans + } +} +``` + @@ -414,6 +474,27 @@ function findKDistantIndices(nums: number[], key: number, k: number): number[] { } ``` +#### Rust + +```rust +impl Solution { + pub fn find_k_distant_indices(nums: Vec, key: i32, k: i32) -> Vec { + let n = nums.len(); + let mut ans = Vec::new(); + let mut j = 0; + for i in 0..n { + while j < i.saturating_sub(k as usize) || (j < n && nums[j] != key) { + j += 1; + } + if j < n && j <= i + k as usize { + ans.push(i as i32); + } + } + ans + } +} +``` + diff --git a/solution/2200-2299/2200.Find All K-Distant Indices in an Array/README_EN.md b/solution/2200-2299/2200.Find All K-Distant Indices in an Array/README_EN.md index 4bc9425ea523a..f8796012d1946 100644 --- a/solution/2200-2299/2200.Find All K-Distant Indices in an Array/README_EN.md +++ b/solution/2200-2299/2200.Find All K-Distant Indices in an Array/README_EN.md @@ -168,6 +168,26 @@ function findKDistantIndices(nums: number[], key: number, k: number): number[] { } ``` +#### Rust + +```rust +impl Solution { + pub fn find_k_distant_indices(nums: Vec, key: i32, k: i32) -> Vec { + let n = nums.len(); + let mut ans = Vec::new(); + for i in 0..n { + for j in 0..n { + if (i as i32 - j as i32).abs() <= k && nums[j] == key { + ans.push(i as i32); + break; + } + } + } + ans + } +} +``` + @@ -307,6 +327,46 @@ function findKDistantIndices(nums: number[], key: number, k: number): number[] { } ``` +#### Rust + +```rust +impl Solution { + pub fn find_k_distant_indices(nums: Vec, key: i32, k: i32) -> Vec { + let n = nums.len(); + let mut idx = Vec::new(); + for i in 0..n { + if nums[i] == key { + idx.push(i as i32); + } + } + + let search = |x: i32| -> usize { + let (mut l, mut r) = (0, idx.len()); + while l < r { + let mid = (l + r) >> 1; + if idx[mid] >= x { + r = mid; + } else { + l = mid + 1; + } + } + l + }; + + let mut ans = Vec::new(); + for i in 0..n { + let l = search(i as i32 - k); + let r = search(i as i32 + k + 1) as i32 - 1; + if l as i32 <= r { + ans.push(i as i32); + } + } + + ans + } +} +``` + @@ -412,6 +472,27 @@ function findKDistantIndices(nums: number[], key: number, k: number): number[] { } ``` +#### Rust + +```rust +impl Solution { + pub fn find_k_distant_indices(nums: Vec, key: i32, k: i32) -> Vec { + let n = nums.len(); + let mut ans = Vec::new(); + let mut j = 0; + for i in 0..n { + while j < i.saturating_sub(k as usize) || (j < n && nums[j] != key) { + j += 1; + } + if j < n && j <= i + k as usize { + ans.push(i as i32); + } + } + ans + } +} +``` + diff --git a/solution/2200-2299/2200.Find All K-Distant Indices in an Array/Solution.rs b/solution/2200-2299/2200.Find All K-Distant Indices in an Array/Solution.rs new file mode 100644 index 0000000000000..3613c2edadca4 --- /dev/null +++ b/solution/2200-2299/2200.Find All K-Distant Indices in an Array/Solution.rs @@ -0,0 +1,15 @@ +impl Solution { + pub fn find_k_distant_indices(nums: Vec, key: i32, k: i32) -> Vec { + let n = nums.len(); + let mut ans = Vec::new(); + for i in 0..n { + for j in 0..n { + if (i as i32 - j as i32).abs() <= k && nums[j] == key { + ans.push(i as i32); + break; + } + } + } + ans + } +} diff --git a/solution/2200-2299/2200.Find All K-Distant Indices in an Array/Solution2.rs b/solution/2200-2299/2200.Find All K-Distant Indices in an Array/Solution2.rs new file mode 100644 index 0000000000000..fcfcdb2776a6a --- /dev/null +++ b/solution/2200-2299/2200.Find All K-Distant Indices in an Array/Solution2.rs @@ -0,0 +1,35 @@ +impl Solution { + pub fn find_k_distant_indices(nums: Vec, key: i32, k: i32) -> Vec { + let n = nums.len(); + let mut idx = Vec::new(); + for i in 0..n { + if nums[i] == key { + idx.push(i as i32); + } + } + + let search = |x: i32| -> usize { + let (mut l, mut r) = (0, idx.len()); + while l < r { + let mid = (l + r) >> 1; + if idx[mid] >= x { + r = mid; + } else { + l = mid + 1; + } + } + l + }; + + let mut ans = Vec::new(); + for i in 0..n { + let l = search(i as i32 - k); + let r = search(i as i32 + k + 1) as i32 - 1; + if l as i32 <= r { + ans.push(i as i32); + } + } + + ans + } +} diff --git a/solution/2200-2299/2200.Find All K-Distant Indices in an Array/Solution3.rs b/solution/2200-2299/2200.Find All K-Distant Indices in an Array/Solution3.rs new file mode 100644 index 0000000000000..25e7e2b6ec360 --- /dev/null +++ b/solution/2200-2299/2200.Find All K-Distant Indices in an Array/Solution3.rs @@ -0,0 +1,16 @@ +impl Solution { + pub fn find_k_distant_indices(nums: Vec, key: i32, k: i32) -> Vec { + let n = nums.len(); + let mut ans = Vec::new(); + let mut j = 0; + for i in 0..n { + while j < i.saturating_sub(k as usize) || (j < n && nums[j] != key) { + j += 1; + } + if j < n && j <= i + k as usize { + ans.push(i as i32); + } + } + ans + } +}