当前位置:网站首页>Leetcode: Shortest Word Distance II
Leetcode: Shortest Word Distance II
2022-07-05 14:56:00 【Full stack programmer webmaster】
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it?
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.
For example,
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
Given word1 = “coding”, word2 = “practice”, return 3.
Given word1 = "makes", word2 = "coding", return 1.
Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.Hash table method
Complexity
Time O(N) Space O(N)
Ideas
Because... Will be called many times , We can't find the subscripts of these two words every time we call . We can use a hash table , When passing in a string array , Use HashMap To store word as well as word stay Array What's in it index. So when the shortest distance method is called , We just need to traverse the subscript list of two words . Specific comparison methods , It's like merge two list, Compare two at a time list The two smallest values , You get a difference . Then remove the smaller one . Because we traverse the input array from front to back , So the subscript list is also orderly .
1 public class WordDistance {
2
3 HashMap<String, ArrayList<Integer>> map;
4
5 public WordDistance(String[] words) {
6 this.map = new HashMap<String, ArrayList<Integer>>();
7 for (int i=0; i<words.length; i++) {
8 String item = words[i];
9 if (map.containsKey(item)) {
10 map.get(item).add(i);
11 }
12 else {
13 ArrayList<Integer> list = new ArrayList<Integer>();
14 list.add(i);
15 map.put(item, list);
16 }
17 }
18 }
19
20 public int shortest(String word1, String word2) {
21 ArrayList<Integer> l1 = map.get(word1);
22 ArrayList<Integer> l2 = map.get(word2);
23 int minDis = Integer.MAX_VALUE;
24 int i=0, j=0;
25 while (i<l1.size() && j<l2.size()) {
26 int p1 = l1.get(i);
27 int p2 = l2.get(j);
28 minDis = Math.min(minDis, Math.abs(p1-p2));
29 if (p1 < p2) i++;
30 else j++;
31 }
32 return minDis;
33 }
34 }
35
36 // Your WordDistance object will be instantiated and called as such:
37 // WordDistance wordDistance = new WordDistance(words);
38 // wordDistance.shortest("word1", "word2");
39 // wordDistance.shortest("anotherWord1", "anotherWord2");Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/109200.html Link to the original text :https://javaforall.cn
边栏推荐
- Is it OK to open the securities account on the excavation finance? Is it safe?
- 亿咖通科技通过ISO27001与ISO21434安全管理体系认证
- 漫画:优秀的程序员具备哪些属性?
- PostgreSQL 13 installation
- maxcompute有没有能查询 表当前存储容量的大小(kb) 的sql?
- Share 20 strange JS expressions and see how many correct answers you can get
- Niuke: intercepting missiles
- 1330:【例8.3】最少步数
- R 熵权法计算权重及综合得分
- CPU design practice - Chapter 4 practical task 2 using blocking technology to solve conflicts caused by related problems
猜你喜欢
随机推荐
申请代码签名证书时如何选择合适的证书品牌?
729. My schedule I: "simulation" & "line segment tree (dynamic open point) &" block + bit operation (bucket Division) "
Under the crisis of enterprise development, is digital transformation the future savior of enterprises
JMeter performance test: serveragent resource monitoring
你童年的快乐,都是被它承包了
详解Vue适时清理keepalive缓存方案
Crud of MySQL
Structure - C language
一键更改多个文件名字
Detailed explanation of usememo, memo, useref and other relevant hooks
R 熵权法计算权重及综合得分
开挖财上的证券账户可以吗?安全吗?
anaconda使用中科大源
How to paste the contents copied by the computer into mobaxterm? How to copy and paste
P6183 [USACO10MAR] The Rock Game S
Visual task scheduling & drag and drop | scalph data integration based on Apache seatunnel
[12 classic written questions of array and advanced pointer] these questions meet all your illusions about array and pointer, come on!
Topology可视化绘图引擎
GPS original coordinates to Baidu map coordinates (pure C code)
如何将电脑复制的内容粘贴进MobaXterm?如何复制粘贴



![[JVM] operation instruction](/img/f5/85580495474ef58eafbb421338e93f.png)




