Skip to content

Commit 029ff28

Browse files
authored
feat: add rust solution to lc problem: No.2515 (doocs#1272)
Signed-off-by: xiaolatiao <1628652790@qq.com>
1 parent 5204794 commit 029ff28

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

solution/2500-2599/2515.Shortest Distance to Target String in a Circular Array/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,29 @@ impl Solution {
198198
}
199199
```
200200

201+
```rust
202+
use std::cmp::min;
203+
204+
impl Solution {
205+
pub fn closet_target(words: Vec<String>, target: String, start_index: i32) -> i32 {
206+
let mut ans = words.len();
207+
208+
for (i, w) in words.iter().enumerate() {
209+
if *w == target {
210+
let t = (i as i32 - start_index).abs();
211+
ans = min(ans, min(t as usize, words.len() - t as usize));
212+
}
213+
}
214+
215+
if ans == words.len() {
216+
return -1;
217+
}
218+
219+
ans as i32
220+
}
221+
}
222+
```
223+
201224
### **C**
202225

203226
```c

solution/2500-2599/2515.Shortest Distance to Target String in a Circular Array/README_EN.md

+23
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,29 @@ impl Solution {
185185
}
186186
```
187187

188+
```rust
189+
use std::cmp::min;
190+
191+
impl Solution {
192+
pub fn closet_target(words: Vec<String>, target: String, start_index: i32) -> i32 {
193+
let mut ans = words.len();
194+
195+
for (i, w) in words.iter().enumerate() {
196+
if *w == target {
197+
let t = (i as i32 - start_index).abs();
198+
ans = min(ans, min(t as usize, words.len() - t as usize));
199+
}
200+
}
201+
202+
if ans == words.len() {
203+
return -1;
204+
}
205+
206+
ans as i32
207+
}
208+
}
209+
```
210+
188211
### **C**
189212

190213
```c

0 commit comments

Comments
 (0)