列表

详情


243. 最短单词距离

给定一个字符串数组 wordDict 和两个已经存在于该数组中的不同的字符串 word1word2 。返回列表中这两个单词之间的最短距离。

 

示例 1:

输入: wordsDict = ["practice", "makes", "perfect", "coding", "makes"], word1 = "coding", word2 = "practice"
输出: 3

示例 2:

输入: wordsDict = ["practice", "makes", "perfect", "coding", "makes"], word1 = "makes", word2 = "coding"
输出: 1

 

提示:

相似题目

最短单词距离 II

最短单词距离 III

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class Solution { public: int shortestDistance(vector<string>& wordsDict, string word1, string word2) { } };

golang 解法, 执行用时: 0 ms, 内存消耗: 4 MB, 提交时间: 2023-10-15 18:58:37

func shortestDistance(wordsDict []string, word1, word2 string) int {
    ans := len(wordsDict)
    index1, index2 := -1, -1
    for i, word := range wordsDict {
        if word == word1 {
            index1 = i
        } else if word == word2 {
            index2 = i
        }
        if index1 >= 0 && index2 >= 0 {
            ans = min(ans, abs(index1-index2))
        }
    }
    return ans
}

func abs(x int) int {
    if x < 0 {
        return -x
    }
    return x
}

func min(a, b int) int {
    if a > b {
        return b
    }
    return a
}

javascript 解法, 执行用时: 68 ms, 内存消耗: 42.1 MB, 提交时间: 2023-10-15 18:58:24

/**
 * @param {string[]} wordsDict
 * @param {string} word1
 * @param {string} word2
 * @return {number}
 */
var shortestDistance = function(wordsDict, word1, word2) {
    const length = wordsDict.length;
    let ans = length;
    let index1 = -1, index2 = -1;
    for (let i = 0; i < length; i++) {
        const word = wordsDict[i];
        if (word === word1) {
            index1 = i;
        } else if (word === word2) {
            index2 = i;
        }
        if (index1 >= 0 && index2 >= 0) {
            ans = Math.min(ans, Math.abs(index1 - index2));
        }
    }
    return ans;
};

java 解法, 执行用时: 2 ms, 内存消耗: 43.5 MB, 提交时间: 2023-10-15 18:58:13

class Solution {
    public int shortestDistance(String[] wordsDict, String word1, String word2) {
        int length = wordsDict.length;
        int ans = length;
        int index1 = -1, index2 = -1;
        for (int i = 0; i < length; i++) {
            String word = wordsDict[i];
            if (word.equals(word1)) {
                index1 = i;
            } else if (word.equals(word2)) {
                index2 = i;
            }
            if (index1 >= 0 && index2 >= 0) {
                ans = Math.min(ans, Math.abs(index1 - index2));
            }
        }
        return ans;
    }
}

cpp 解法, 执行用时: 4 ms, 内存消耗: 11.6 MB, 提交时间: 2023-10-15 18:58:00

class Solution {
public:
    int shortestDistance(vector<string>& wordsDict, string word1, string word2) {
        int length = wordsDict.size();
        int ans = length;
        int index1 = -1, index2 = -1;
        for (int i = 0; i < length; i++) {
            if (wordsDict[i] == word1) {
                index1 = i;
            } else if (wordsDict[i] == word2) {
                index2 = i;
            }
            if (index1 >= 0 && index2 >= 0) {
                ans = min(ans, abs(index1 - index2));
            }
        }
        return ans;
    }
};

python3 解法, 执行用时: 44 ms, 内存消耗: 18.8 MB, 提交时间: 2023-10-15 18:57:48

class Solution:
    def shortestDistance(self, wordsDict: List[str], word1: str, word2: str) -> int:
        ans = len(wordsDict)
        index1, index2 = -1, -1
        for i, word in enumerate(wordsDict):
            if word == word1:
                index1 = i
            elif word == word2:
                index2 = i
            if index1 >= 0 and index2 >= 0:
                ans = min(ans, abs(index1 - index2))
        return ans

上一题