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


相关推荐

  • JDK1.8 ArrayList 扩容详解

    JDK1.8 ArrayList 扩容详解arraylist这个数据结构比较简单,总体来说,arraylist底层结构是数组,他的很多方法都是从数组上面演变而来的,下面分析下arraylist的扩容机制,每次在add()一个元素时,arraylist都需要对这个list的容量进行一个判断。如果容量够,直接添加,否则需要进行扩容。在1.8arraylist这个类中,扩容调用的是grow()方法,通过grow()方法中调用的Array…

    2022年5月22日
    38
  • SQL server2019安装教程[通俗易懂]

    下载必备由于安装文件太大,所以没有办法上传,各位就请自行下载安装步骤点击sql2019安装包选择基本浏览安装到指定的位置,点击安装下载下载完成之后就是这个界面,然后点击自定义选择下一步等待扫描完成后选择选择下一步之后再选择第一个“执行SQLSERVER2019的全新安装”,然后点击下一步勾选第一个接受条款,继续下一步在“实例功能”里面勾选你需要…

    2022年4月7日
    59
  • ORM框架Hibernate (二) 持久化对象的三种状态分析

    ORM框架Hibernate (二) 持久化对象的三种状态分析

    2021年8月25日
    53
  • APP推送系统工作原理

    APP推送系统工作原理一、传统APP架构下的信息传送APP主动向服务器请求数据,服务器被动的提供数据。步骤如下:然而,如果此时服务器又有了新的新闻,在用户没有主动刷新的情况下,服务器是不会主动推送给用户的。推送解决了这个困境,它让服务器主动连接APP,通知APP有了新的新闻,可以再请求。收到推送的APP(即使已关闭)又去服务器请求最新的新闻,用户就能看到了。二、实现推送的方法实现一个推送系统需要服务器端和…

    2022年6月2日
    29
  • Java调用so文件[通俗易懂]

    Java调用so文件[通俗易懂]公司的硬件让我帮忙调用一个so文件,想着一直都没机会自己写一个jni,于是就答应了,在调用的过程中还踩了不少坑,特地写一篇博客记录一下。一、使用技术原本是想直接用java自带的jni,但是我们硬件只给了一个so文件,而且里面的函数命名等规则不符合java的jni调用标准,于是就打算使用框架jna来调用。JNA就是建立在JNI之上,它简化了Java调用原生函数的过程。JNA提供了一…

    2022年9月19日
    0
  • 音视频协议-RTP协议

    音视频协议-RTP协议1协议简介2协议格式介绍3协议解析4协议三方库使用

    2022年6月28日
    40

发表回复

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

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