Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 43be834

Browse files
solves #2515: Shortest Distance to Target String in a Circular Array in java
1 parent 983e2ea commit 43be834

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@
790790
| 2500 | [Delete Greatest Value in Each Row](https://leetcode.com/problems/delete-greatest-value-in-each-row) | [![Java](assets/java.png)](src/DeleteGreatestValueInEachRow.java) | |
791791
| 2506 | [Count Pairs Of Similar Strings](https://leetcode.com/problems/count-pairs-of-similar-strings) | [![Java](assets/java.png)](src/CountPairsOfSimilarStrings.java) | |
792792
| 2511 | [Maximum Enemy Forts That Can Be Captured](https://leetcode.com/problems/maximum-enemy-forts-that-can-be-captured) | [![Java](assets/java.png)](src/MaximumEnemyFortsThatCanBeCaptured.java) | |
793-
| 2515 | [Shortest Distance to Target String in a Circular Array](https://leetcode.com/problems/shortest-distance-to-target-string-in-a-circular-array) | | |
793+
| 2515 | [Shortest Distance to Target String in a Circular Array](https://leetcode.com/problems/shortest-distance-to-target-string-in-a-circular-array) | [![Java](assets/java.png)](src/ShortestDistanceToTargetStringInACircularArray.java) | |
794794
| 2520 | [Count the Digits That Divide a Number](https://leetcode.com/problems/count-the-digits-that-divide-a-number) | | |
795795
| 2525 | [Categorize Box According to Criteria](https://leetcode.com/problems/categorize-box-according-to-criteria) | | |
796796
| 2529 | [Maximum Count of Positive Integer and Negative Integer](https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer) | | |
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// https://leetcode.com/problems/shortest-distance-to-target-string-in-a-circular-array
2+
// T: O(N)
3+
// S: O(1)
4+
5+
public class ShortestDistanceToTargetStringInACircularArray {
6+
public int closetTarget(String[] words, String target, int startIndex) {
7+
if (words[startIndex].equals(target)) return 0;
8+
9+
for (int left = (startIndex - 1 + words.length) % words.length, right = (startIndex + 1) % words.length, count = 1;
10+
count <= words.length / 2;
11+
left = (left - 1 + words.length) % words.length, right = (right + 1) % words.length, count++
12+
) {
13+
if (words[left].equals(target) || words[right].equals(target)) return count;
14+
}
15+
return -1;
16+
}
17+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /