当前位置:网站首页>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
边栏推荐
- 裁员下的上海
- 【招聘岗位】基础设施软件开发人员
- 我想咨询一下,mysql一个事务对于多张表的更新,怎么保证数据一致性的?
- Reconnaissance des caractères easycr
- MySQL----函数
- [recruitment position] Software Engineer (full stack) - public safety direction
- Machine learning notes - gray wolf optimization
- Long list optimized virtual scrolling
- I want to inquire about how to ensure data consistency when a MySQL transaction updates multiple tables?
- Cartoon: what are the attributes of a good programmer?
猜你喜欢

Super wow fast row, you are worth learning!

Security analysis of Web Architecture

Photoshop plug-in action related concepts actionlist actiondescriptor actionlist action execution load call delete PS plug-in development

Topology visual drawing engine

Two Bi development, more than 3000 reports? How to do it?

There is a powerful and good-looking language bird editor, which is better than typora and developed by Alibaba

MongDB学习笔记

Crud of MySQL

Interview shock 62: what are the precautions for group by?

leetcode:881. lifeboat
随机推荐
MySQL之CRUD
Type declaration of all DOM elements in TS
Install and configure Jenkins
mysql8.0JSON_ Instructions for using contains
【招聘岗位】基础设施软件开发人员
微帧科技荣获全球云计算大会“云鼎奖”!
mysql8.0JSON_CONTAINS的使用说明
[recruitment position] infrastructure software developer
通过npm 或者 yarn安装依赖时 报错 出现乱码解决方式
黑马程序员-软件测试-10阶段2-linux和数据库-44-57为什么学习数据库,数据库分类关系型数据库的说明Navicat操作数据的说明,Navicat操作数据库连接说明,Navicat的基本使用,
TS所有dom元素的类型声明
基于TI DRV10970驱动直流无刷电机
Explain Vue's plan to clean up keepalive cache in time
P6183 [USACO10MAR] The Rock Game S
市值蒸发超百亿美元,“全球IoT云平台第一股”赴港求生
安装配置Jenkins
Anaconda uses China University of science and technology source
STM32+BH1750光敏传感器获取光照强度
Interpretation of Apache linkage parameters in computing middleware
P1451 求细胞数量/1329:【例8.2】细胞