Leetcode: Shortest Word Distance II

Leetcode: Shortest Word Distance II

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.

哈希表法

复杂度

时间 O(N) 空间 O(N)

思路

因为会多次调用,我们不能每次调用的时候再把这两个单词的下标找出来。我们可以用一个哈希表,在传入字符串数组时,使用HashMap来储存word以及word在Array里面出现的index。这样当调用最短距离的方法时,我们只要遍历两个单词的下标列表就行了。具体的比较方法,则类似merge two list,每次比较两个list最小的两个值,得到一个差值。然后把较小的那个给去掉。因为我们遍历输入数组时是从前往后的,所以下标列表也是有序的。

 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");

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/109200.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • bootstrap开发工具_bootstrap经典网页布局

    bootstrap开发工具_bootstrap经典网页布局在这里推荐一款基于bootstrap的在线布局工具,以后布局可以就可以和做PPT一样了http://www.bootcss.com/p/layoutit/

    2022年8月3日
    5
  • 网络系统渗透测试步骤_网络安全工程师日常工作内容

    网络系统渗透测试步骤_网络安全工程师日常工作内容100渗透测试工具使用方法介绍

    2022年8月12日
    6
  • Dijkstra算法

    Dijkstra算法

    2021年12月3日
    49
  • python jinja2_Python模块学习 – jinja2

    python jinja2_Python模块学习 – jinja2转置 https www cnblogs com dachenzi p 8242713 html 模板要了解 jinja2 那么需要先理解模板的概念 模板在 Python 的 web 开发中广泛使用 它能够有效的将业务逻辑和页面逻辑分开 使代码可读性增强 并且更加容易理解和维护 模板简单来说就是一个其中包涵占位变量表示动态的部分的文件 模板文件在经过动态赋值后 返回给用户 gt 可以理解为渲染

    2025年5月21日
    5
  • SQLyog安装教程详解

    SQLyog安装教程详解安装SQLyog的详细步骤(1)复制连接:https://pan.baidu.com/s/1IlkLChap1gYzCHo3meegew输入提取码:a1kw(2)等待下载(3)解压到新建文件夹(4)点击解压后的X64右键,以管理员的身份运行(5)选择语言Chinese(Simplified)(6)单击下一步(7)打开后需要证书姓名(Name):cr173序列号(Code):8d8120df-a5c3-4989-8f47-5afc79c56e7c或者(OR)姓名

    2022年5月28日
    51
  • 栈和队列讲解_栈和队列的优缺点

    栈和队列讲解_栈和队列的优缺点目录1、栈(1)栈的概念及结构(2)栈的实现2、队列(1)队列的概念及结构(2)队列的实现前言:栈和队列是在顺序表和链表的延伸,如果前面的顺序表和链表你已经掌握了的话,栈和队列对你来说应该就是小菜一碟了。1、栈(1)栈的概念及结构栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(LastInFirstOut)的原则。压栈:栈的插入操作叫做进栈/压栈..

    2025年6月22日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号