[LeetCode] 243, 244, 245. Shortest Word Distance I, II, III

这三道题差别不是非常大所以可以放在一起练习。

243. Shortest Word Distance

Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.

Example:
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].

“coding”
“practice”
"makes"
"coding"

Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.

题意是给一个words列表,请你返回其中两个单词word1和word2之间的下标差。这两个单词一定是不同的

时间O(n)

空间O(1)

Java实现

复制代码
 1 class Solution {
 2     public int shortestDistance(String[] words, String word1, String word2) {
 3         int res = words.length;
 4         int a = -1;
 5         int b = -1;
 6         for (int i = 0; i < words.length; i++) {
 7             if (words[i].equals(word1)) {
 8                 a = i;
 9             } else if (words[i].equals(word2)) {
10                 b = i;
11             }
12             if (a != -1 && b != -1) {
13                 res = Math.min(res, Math.abs(a - b));
14             }
15         }
16         return res;
17     }
18 }
复制代码

 

244. Shortest Word Distance II

Design a class which receives a list of words in the constructor, and implements a method that takes two words word1 and word2 and return the shortest distance between these two words in the list. Your method will be called repeatedly many times with different parameters. 

Example:
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].

“coding”
“practice”
"makes"
"coding"

Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.

题意是给一个单词列表,请你设计一个class,还是计算跟243题类似的单词之间的最短距离。
思路是创建一个hashmap<单词,List<单词的下标>>,记录每个不同的单词和每个单词出现的下标。在计算最短距离的时候,遍历两个单词的List<下标>,以得出最短距离。这道题没说word1和word2是否一样,但是从结果来看应该是无所谓的,如果一样,最短距离就是0。
时间O(n^2)
空间O(n)
Java实现
复制代码
 1 class WordDistance {
 2     HashMap<String, List<Integer>> map;
 3 
 4     public WordDistance(String[] words) {
 5         map = new HashMap<>();
 6         for (int i = 0; i < words.length; i++) {
 7             if (map.containsKey(words[i])) {
 8                 map.get(words[i]).add(i);
 9             } else {
10                 map.put(words[i], new ArrayList<>());
11                 map.get(words[i]).add(i);
12             }
13         }
14     }
15     
16     public int shortest(String word1, String word2) {
17         List<Integer> l1 = map.get(word1);
18         List<Integer> l2 = map.get(word2);
19         int res = Integer.MAX_VALUE;
20         for (Integer num1 : l1) {
21             for (Integer num2 : l2) {
22                 res = Math.min(res, Math.abs(num1 - num2));
23             }
24         }
25         return res;
26     }
27 }
28 
29 /**
30  * Your WordDistance object will be instantiated and called as such:
31  * WordDistance obj = new WordDistance(words);
32  * int param_1 = obj.shortest(word1,word2);
33  */
复制代码

 

245. Shortest Word Distance III

Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.

word1 and word2 may be the same and they represent two individual words in the list.

Example:
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].

“makes”
“coding”
"makes"
"makes"

Note:
You may assume word1 and word2 are both in the list.

245题与前两题的区别在于word1和word2有可能是相同的。当然,如果他们相同的话,意味着这个单词出现了多次。还是找最短距离。

时间O(n)

空间O(1)

Java实现

复制代码
 1 class Solution {
 2     public int shortestWordDistance(String[] words, String word1, String word2) {
 3         int p1 = -1;
 4         int p2 = -1;
 5         int distance = words.length;
 6         for (int i = 0; i < words.length; i++) {
 7             if (words[i].equals(word1)) {
 8                 p1 = i;
 9                 if (p1 != -1 && p2 != -1) {
10                     distance = (p1 != p2) ? Math.min(distance, Math.abs(p1 - p2)) : distance;
11                 }
12             }
13             if (words[i].equals(word2)) {
14                 p2 = i;
15                 if (p1 != -1 && p2 != -1) {
16                     distance = (p1 != p2) ? Math.min(distance, Math.abs(p1 - p2)) : distance;
17                 }
18             }
19         }
20         return distance;
21     }
22 }
复制代码

 

LeetCode 题目总结

posted @   CNoodle  阅读(273)  评论(0编辑  收藏  举报
编辑推荐:
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
阅读排行:
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥
历史上的今天:
2019-11-04 [LeetCode] 33. Search in Rotated Sorted Array
2019-11-04 [LeetCode] 154. Find Minimum in Rotated Sorted Array II
2019-11-04 [LeetCode] 153. Find Minimum in Rotated Sorted Array
2019-11-04 [LeetCode] 162. Find Peak Element
2019-11-04 [LeetCode] 34. Find First and Last Position of Element in Sorted Array
点击右上角即可分享
微信分享提示