最短单词距离系列(243 244 245)

243. 最短单词距离

给定一个单词列表和两个单词 word1word2,返回列表中这两个单词之间的最短距离。

示例:
假设 words = ["practice", "makes", "perfect", "coding", "makes"]

输入: word1 = “coding”, word2 = “practice”
输出: 3
输入: word1 = "makes", word2 = "coding"
输出: 1

注意:
你可以假设 word1 不等于 word2, 并且 word1word2 都在列表里。

解法:双指针

思想

用两个变量记录数组元素与 word1word2 相同时的下标

复杂度

时间复杂度: O ( N ) O(N) O(N) N N Nwords 长度

空间复杂度: N ( 1 ) N(1) N(1)

代码

class Solution {
    public int shortestDistance(String[] words, String word1, String word2) {
        int l = -1, r = -1, n = words.length;
        int res = Integer.MAX_VALUE;
        
        for (int i = 0; i < n; i++) {
            if (words[i].equals(word1)) {
                l = i;
            } else if (words[i].equals(word2)) {
                r = i;
            }
            if (l != -1 && r != -1) {
                res = Math.min(res, Math.abs(l - r));
            }
        }
 
        return res;
    }
}

244. 最短单词距离 II

请设计一个类,使该类的构造函数能够接收一个单词列表。然后再实现一个方法,该方法能够分别接收两个单词 word1 和 *word2,*并返回列表中这两个单词之间的最短距离。您的方法将被以不同的参数调用 多次

示例:
假设 words = ["practice", "makes", "perfect", "coding", "makes"]

输入: word1 = “coding”, word2 = “practice”
输出: 3
输入: word1 = "makes", word2 = "coding"
输出: 1

注意:
你可以假设 word1 不等于 word2, 并且 word1word2 都在列表里。

解法:哈希表

思想

以单词为 key,以在数组中元素下标为 value,存入哈希表中。采用拉链法挂相同的 key

复杂度

时间复杂度:构造函数 O ( N ) O(N) O(N)shortest O ( m a x { K , L } ) , O(max \{ K, L\}), O(max{K,L}) N N Nwords 长度, K K K L L L 分别为两个单词对应的下标数组的长度。

空间复杂度: O ( N ) O(N) O(N)

代码

class WordDistance {
    private Map<String, List<Integer>> map;

    public WordDistance(String[] words) {
        map = new HashMap<>();
        int n = words.length;
        for (int i = 0; i < n; i++) {
            if (map.containsKey(words[i])) {
                map.get(words[i]).add(i);
            } else {
                List<Integer> list = new ArrayList<>();
                list.add(i);
                map.put(words[i], list);
            }
        }
    }
    
    public int shortest(String word1, String word2) {
        int res = Integer.MAX_VALUE;
        List<Integer> l1 = map.get(word1);
        List<Integer> l2 = map.get(word2);
        int n = l1.size(), m = l2.size();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                res = Math.min(res, Math.abs(l1.get(i) - l2.get(j)));
            }
        }
        return res;
    }
}

245. 最短单词距离 III

给定一个字符串数组 words 和两个字符串 word1word2 ,返回列表中这两个单词之间的最短距离。

注意:word1word2 是有可能相同的,并且它们将分别表示为列表中 两个独立的单词

解法

思想:双指针

243. 最短单词距离 区别是 word1word2 可能相同,所以只需要处理这种特殊情况就行。

复杂度

时间复杂度: O ( N ) O(N) O(N) N N Nwords 长度

空间复杂度: O ( 1 ) O(1) O(1)

代码

class Solution {
    public int shortestWordDistance(String[] words, String word1, String word2) {
        int l = -1, r = -1, n = words.length;
        int res = Integer.MAX_VALUE;
        
        if (word1.equals(word2)) {
            for (int i = 0; i < n; i++) {
                if (words[i].equals(word1)) {
                    l = r;
                    r = i;
                    if (l != -1 && r != -1) {
                        res = Math.min(res, Math.abs(l - r));
                    }
                }
            }
        } else {
            for (int i = 0; i < n; i++) {
                if (words[i].equals(word1)) {
                    l = i;
                } else if (words[i].equals(word2)) {
                    r = i;
                }
                if (l != -1 && r != -1) {
                    res = Math.min(res, Math.abs(l - r));
                }            
            }
        }

        return res;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值