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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ES6中变量let的属性以及在for循环中的使用

    ES6中变量let的属性以及在for循环中的使用

    2022年4月3日
    130
  • 转:SIGPIPE[通俗易懂]

    转:SIGPIPE[通俗易懂]SIGPIPEsend或者writesocket遭遇SIGPIPE信号当服务器close一个连接时,若client端接着发数据。根据TCP协议的规定,会收到一个RST响应,client再往这个服务器发送数据时,系统会发出一个SIGPIPE信号给进程,告诉进程这个…

    2022年5月30日
    32
  • vim复制粘贴命令

    vim复制粘贴命令1.选定文本块。使用v进入可视模式,移动光标键选定内容。 2.复制的命令是y,即yank(提起),常用的命令如下:   y   在使用v模式选定了某一块的时候,复制选定块到缓冲区用;   yy  复制整行(nyy或者yny,复制n行,n为数字);   y^  复制当前到行头的内容;   y$  复制当前到行尾的内容;   yw  复制一个word(ny…

    2022年9月23日
    3
  • 【Ubuntu 】Ubuntu 更换国内源—解决终端下载错误或速度慢的问题「建议收藏」

    【Ubuntu 】Ubuntu 更换国内源—解决终端下载错误或速度慢的问题「建议收藏」前言:安装好ubuntu双系统后,默认的软件更新源是国外的,在国内使用速度很慢,用”aptinstallxxx”安装软件时可能出现”网络不可达”、”你的网络需要认证吗”、”无法定位软件包”等错误,所以我们需要更换成国内的源,这样才能正常安装和更新软件。目录一、ubuntu16.04更换国内源二、ubuntu18.04更换国内源三、UbuntuROS更换国内源

    2022年5月14日
    48
  • Idea配置SVN教程

    Idea配置SVN教程第一步:下载svn的客户端,通俗一点来说就是小乌龟啦!去电脑管理的软件管理里面可以直接下载,方便迅速下载之后直接安装就好了,但是要注意这里的这个文件也要安装上,默认是不安装的,如果不安装,svn中的bin目录下就会没有svn.exe,这个待会会用到,所以一点要注意哦。(都是坑啊)如果你不幸没点第二个,别着急,本文章末尾给你解决办法然后就下一步下一步就安装好了。配置svn的环境变量在此就…

    2022年5月14日
    242
  • cmd批处理命令~%dp0与~%dpn1的解析

    cmd批处理命令~%dp0与~%dpn1的解析1、最简单的做法是在cmd命令输入:for/?,回车,就能看到详细的解析对一组文件中的每一个文件执行某个特定命令。FOR%variableIN(set)DOcommand[command-parameters]%variable指定一个单一字母可替换的参数。(set)指定一个或一组文件。可以使用通配符。command指定对每个文件执行的命令。…

    2022年9月16日
    4

发表回复

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

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