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


相关推荐

  • 基于Eclipse的Android开发环境搭建「建议收藏」

    基于Eclipse的Android开发环境搭建「建议收藏」对于才接触Android开发的人员来说,复杂的开发环境搭建,各种新知识,新内容要接触总是让人无从下手。通过这篇文章,让零基础开发人员成功搭建基于Eclipse的android开发环境。Android使用Java语言进行开发,首先我们需要搭建Java开发环境,在此基础上搭建Android开发环境。如果开发者有Java开发经验,并有基于Eclipse的Java开发环境,可以跳过Java开发环境搭建。

    2022年7月23日
  • artcam浮雕实例教程_artcam2018入门教程

    artcam浮雕实例教程_artcam2018入门教程记录过去,奋斗现在,展望未来(给未来即将结束的工作的’存档’)ArtCAM入门简单教程二、浮雕前言:这不是个很专业的浮雕设计教程,不过是很简单即可实现的入门教程,而更深入的浮雕学习建议查找更全面的网上教程或CAM浮雕书籍。   常用的矢量雕刻,详见:http://blog.sina.com.cn/s/blog_647ef76d0101jgwn.html开始之

    2022年9月5日
  • mysql8.0性能_oracle scan ip

    mysql8.0性能_oracle scan ipMySQL8.0 – 新特性 – Skip Scan Range

    2022年4月21日
  • Linux权限详解(chmod、600、644、700、711、755、777、4755、6755、7755)「建议收藏」

    Linux权限详解(chmod、600、644、700、711、755、777、4755、6755、7755)「建议收藏」权限简介Linux系统上对文件的权限有着严格的控制,用于如果相对某个文件执行某种操作,必须具有对应的权限方可执行成功。 Linux下文件的权限类型一般包括读,写,执行。对应字母为r、w、x。 Linux下权限的粒度有拥有者、群组、其它组三种。每个文件都可以针对三个粒度,设置不同的rwx(读写执行)权限。通常情况下,一个文件只能归属于一个用户和组,如果其它的用户想有这个文件的权限…

    2022年4月27日
  • mysql、Java、mabatis的字段映射

    mysql、Java、mabatis的字段映射mysql、Java、mabatis的字段映射

    2022年4月23日
  • 怎么升级linux的内核版本,升级linux内核版本[通俗易懂]

    怎么升级linux的内核版本,升级linux内核版本[通俗易懂]1、安装公钥sudorpm–importhttps://www.elrepo.org/RPM-GPG-KEY-elrepo.org2、下载内核sudorpm-Uvhhttp://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm3、查看内核版本sudoyum-y–disablerepo=”*”–enabler…

    2022年8月23日

发表回复

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

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