
题目描述
给你一个下标从 0 开始的 环形 字符串数组 words 和一个字符串 target 。环形数组 意味着数组首尾相连。
- 形式上,
words[i] 的下一个元素是 words[(i + 1) % n] ,而 words[i] 的前一个元素是 words[(i - 1 + n) % n] ,其中 n 是 words 的长度。
从 startIndex 开始,你一次可以用 1 步移动到下一个或者前一个单词。
返回到达目标字符串 target 所需的最短距离。如果 words 中不存在字符串 target ,返回 -1 。
示例 1:
输入:words = ["hello","i","am","leetcode","hello"], target = "hello", startIndex = 1
输出:1
解释:从下标 1 开始,可以经由以下步骤到达 "hello" :
- 向右移动 3 个单位,到达下标 4 。
- 向左移动 2 个单位,到达下标 4 。
- 向右移动 4 个单位,到达下标 0 。
- 向左移动 1 个单位,到达下标 0 。
到达 "hello" 的最短距离是 1 。
示例 2:
输入:words = ["a","b","leetcode"], target = "leetcode", startIndex = 0
输出:1
解释:从下标 0 开始,可以经由以下步骤到达 "leetcode" :
- 向右移动 2 个单位,到达下标 2 。
- 向左移动 1 个单位,到达下标 2 。
到达 "leetcode" 的最短距离是 1 。
示例 3:
输入:words = ["i","eat","leetcode"], target = "ate", startIndex = 0
输出:-1
解释:因为 words 中不存在字符串 "ate" ,所以返回 -1 。
提示:
1 <= words.length <= 100 1 <= words[i].length <= 100 words[i] 和 target 仅由小写英文字母组成 0 <= startIndex < words.length
解法
方法一:一次遍历
我们遍历数组 \(\textit{words}\),找到与 \(\textit{target}\) 相等的单词,计算其与 \(\textit{startIndex}\) 的距离 \(t\),则此时的最短距离为 \(\min(t, n - t)\),我们只需要不断更新最小值即可。
时间复杂度 \(O(n)\),其中 \(n\) 是数组的长度。空间复杂度 \(O(1)\)。
| class Solution:
def closestTarget(self, words: List[str], target: str, startIndex: int) -> int:
n = len(words)
ans = n
for i, w in enumerate(words):
if w == target:
t = abs(i - startIndex)
ans = min(ans, t, n - t)
return -1 if ans == n else ans
|
1
2
3
4
5
6
7
8
9
10
11
12
13 | class Solution {
public int closestTarget(String[] words, String target, int startIndex) {
int n = words.length;
int ans = n;
for (int i = 0; i < n; i++) {
if (words[i].equals(target)) {
int t = Math.abs(i - startIndex);
ans = Math.min(ans, Math.min(t, n - t));
}
}
return ans == n ? -1 : ans;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | class Solution {
public:
int closestTarget(vector<string>& words, string target, int startIndex) {
int n = words.size();
int ans = n;
for (int i = 0; i < n; i++) {
if (words[i] == target) {
int t = abs(i - startIndex);
ans = min({ans, t, n - t});
}
}
return ans == n ? -1 : ans;
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 | func closestTarget(words []string, target string, startIndex int) int {
n := len(words)
ans := n
for i, w := range words {
if w == target {
t := i - startIndex
if t < 0 {
t = -t
}
ans = min(ans, t, n-t)
}
}
if ans == n {
return -1
}
return ans
}
|
| function closestTarget(words: string[], target: string, startIndex: number): number {
const n = words.length;
let ans = n;
for (let i = 0; i < n; i++) {
if (words[i] === target) {
const t = Math.abs(i - startIndex);
ans = Math.min(ans, t, n - t);
}
}
return ans === n ? -1 : ans;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13 | impl Solution {
pub fn closest_target(words: Vec<String>, target: String, start_index: i32) -> i32 {
let n = words.len() as i32;
let mut ans = n;
for (i, w) in words.iter().enumerate() {
if w == &target {
let t = (i as i32 - start_index).abs();
ans = ans.min(t.min(n - t));
}
}
if ans == n { -1 } else { ans }
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | #define min(a, b) ((a) < (b) ? (a) : (b))
int closestTarget(char** words, int wordsSize, char* target, int startIndex) {
int n = wordsSize;
int ans = n;
for (int i = 0; i < n; i++) {
if (strcmp(words[i], target) == 0) {
int t = abs(i - startIndex);
int dist = min(t, n - t);
ans = min(ans, dist);
}
}
return ans == n ? -1 : ans;
}
|