Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 149b759

Browse files
add 2515
1 parent 66cf8f1 commit 149b759

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|------|----------------|------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|----------------------------------|-------------
11-
| 2496 |[Maximum Value of a String in an Array](https://leetcode.com/problems/maximum-value-of-a-string-in-an-array/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2496.java) || Easy ||
11+
| 2515 |[Shortest Distance to Target String in a Circular Array](https://leetcode.com/problems/shortest-distance-to-target-string-in-a-circular-array/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2515.java) || Easy ||
12+
| 2496 |[Maximum Value of a String in an Array](https://leetcode.com/problems/maximum-value-of-a-string-in-an-array/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2496.java) || Easy ||
1213
| 2467 |[Convert the Temperature](https://leetcode.com/problems/convert-the-temperature/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2469.java) || Easy ||
1314
| 2455 |[Average Value of Even Numbers That Are Divisible by Three](https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2455.java) || Easy ||
1415
| 2432 |[The Employee That Worked on the Longest Task](https://leetcode.com/problems/the-employee-that-worked-on-the-longest-task/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2432.java) || Easy ||
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _2515 {
4+
public static class Solution1 {
5+
public int closetTarget(String[] words, String target, int startIndex) {
6+
int ans = words.length;
7+
if (words[startIndex].equals(target)) {
8+
return 0;
9+
}
10+
//move forward
11+
int forwardSteps = 1;
12+
for (int i = (startIndex + 1) % words.length; i != startIndex; i = ((i + 1) % words.length)) {
13+
if (words[i].equals(target)) {
14+
ans = Math.min(ans, forwardSteps);
15+
break;
16+
}
17+
forwardSteps++;
18+
}
19+
//move backward
20+
int backwardSteps = 1;
21+
for (int i = (startIndex - 1 + words.length) % words.length; i != startIndex; i = ((i - 1 + words.length) % words.length)) {
22+
if (words[i].equals(target)) {
23+
ans = Math.min(ans, backwardSteps);
24+
break;
25+
}
26+
backwardSteps++;
27+
}
28+
return ans == words.length ? -1 : ans;
29+
}
30+
}
31+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._2515;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _2515Test {
10+
private static _2515.Solution1 solution1;
11+
private static String[] words;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _2515.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
words = new String[]{"hello", "i", "am", "leetcode", "hello"};
21+
assertEquals(1, solution1.closetTarget(words, "hello", 1));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
words = new String[]{"a", "b", "leetcode"};
27+
assertEquals(1, solution1.closetTarget(words, "leetcode", 0));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
words = new String[]{"i", "eat", "leetcode"};
33+
assertEquals(-1, solution1.closetTarget(words, "ate", 0));
34+
}
35+
36+
}

0 commit comments

Comments
 (0)