Skip to content

Commit b75db0f

Browse files
committed
feat: add solutions to lc problem: No.2515
No.2515.Shortest Distance to Target String in a Circular Array
1 parent 1b75c0c commit b75db0f

File tree

5 files changed

+144
-0
lines changed

5 files changed

+144
-0
lines changed

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,59 @@ func min(a, b int) int {
153153
}
154154
```
155155

156+
### **TypeScript**
157+
158+
```ts
159+
function closetTarget(
160+
words: string[],
161+
target: string,
162+
startIndex: number,
163+
): number {
164+
const n = words.length;
165+
for (let i = 0; i <= n >> 1; i++) {
166+
if (
167+
words[(startIndex - i + n) % n] === target ||
168+
words[(startIndex + i) % n] === target
169+
) {
170+
return i;
171+
}
172+
}
173+
return -1;
174+
}
175+
```
176+
177+
### **Rust**
178+
179+
```rust
180+
impl Solution {
181+
pub fn closet_target(words: Vec<String>, target: String, start_index: i32) -> i32 {
182+
let start_index = start_index as usize;
183+
let n = words.len();
184+
for i in 0..=n >> 1 {
185+
if words[(start_index - i + n) % n] == target || words[(start_index + i) % n] == target
186+
{
187+
return i as i32;
188+
}
189+
}
190+
-1
191+
}
192+
}
193+
```
194+
195+
### **C**
196+
197+
```c
198+
int closetTarget(char **words, int wordsSize, char *target, int startIndex) {
199+
for (int i = 0; i <= wordsSize >> 1; i++) {
200+
if (strcmp(words[(startIndex - i + wordsSize) % wordsSize], target) == 0 ||
201+
strcmp(words[(startIndex + i) % wordsSize], target) == 0) {
202+
return i;
203+
}
204+
}
205+
return -1;
206+
}
207+
```
208+
156209
### **...**
157210
158211
```

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,59 @@ func min(a, b int) int {
146146
}
147147
```
148148

149+
### **TypeScript**
150+
151+
```ts
152+
function closetTarget(
153+
words: string[],
154+
target: string,
155+
startIndex: number,
156+
): number {
157+
const n = words.length;
158+
for (let i = 0; i <= n >> 1; i++) {
159+
if (
160+
words[(startIndex - i + n) % n] === target ||
161+
words[(startIndex + i) % n] === target
162+
) {
163+
return i;
164+
}
165+
}
166+
return -1;
167+
}
168+
```
169+
170+
### **Rust**
171+
172+
```rust
173+
impl Solution {
174+
pub fn closet_target(words: Vec<String>, target: String, start_index: i32) -> i32 {
175+
let start_index = start_index as usize;
176+
let n = words.len();
177+
for i in 0..=n >> 1 {
178+
if words[(start_index - i + n) % n] == target || words[(start_index + i) % n] == target
179+
{
180+
return i as i32;
181+
}
182+
}
183+
-1
184+
}
185+
}
186+
```
187+
188+
### **C**
189+
190+
```c
191+
int closetTarget(char **words, int wordsSize, char *target, int startIndex) {
192+
for (int i = 0; i <= wordsSize >> 1; i++) {
193+
if (strcmp(words[(startIndex - i + wordsSize) % wordsSize], target) == 0 ||
194+
strcmp(words[(startIndex + i) % wordsSize], target) == 0) {
195+
return i;
196+
}
197+
}
198+
return -1;
199+
}
200+
```
201+
149202
### **...**
150203
151204
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
int closetTarget(char **words, int wordsSize, char *target, int startIndex) {
2+
for (int i = 0; i <= wordsSize >> 1; i++) {
3+
if (strcmp(words[(startIndex - i + wordsSize) % wordsSize], target) == 0 ||
4+
strcmp(words[(startIndex + i) % wordsSize], target) == 0) {
5+
return i;
6+
}
7+
}
8+
return -1;
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
impl Solution {
2+
pub fn closet_target(words: Vec<String>, target: String, start_index: i32) -> i32 {
3+
let start_index = start_index as usize;
4+
let n = words.len();
5+
for i in 0..=n >> 1 {
6+
if words[(start_index - i + n) % n] == target || words[(start_index + i) % n] == target
7+
{
8+
return i as i32;
9+
}
10+
}
11+
-1
12+
}
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function closetTarget(
2+
words: string[],
3+
target: string,
4+
startIndex: number,
5+
): number {
6+
const n = words.length;
7+
for (let i = 0; i <= n >> 1; i++) {
8+
if (
9+
words[(startIndex - i + n) % n] === target ||
10+
words[(startIndex + i) % n] === target
11+
) {
12+
return i;
13+
}
14+
}
15+
return -1;
16+
}

0 commit comments

Comments
 (0)