原题链接在这里:https://leetcode.com/problems/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.

题解:

method 会被call好多次,没有必要每次都traverse 一遍words array. 简历一个HashMap, key是word, value是该word出现index的list.

双指针遍历word1 和 word2 两个index list 计算最小距离.

Time Complexity: Constructor WordDistance O(words.length). shortest O(list1.size()+list2.size()).

Space: O(words.length).

AC Java:

 public class WordDistance {

     HashMap<String, List<Integer>> hm;
public WordDistance(String[] words) {
hm = new HashMap<String, List<Integer>>();
for(int i = 0; i<words.length; i++){
if(!hm.containsKey(words[i])){
List<Integer> ls = new ArrayList<Integer>();
hm.put(words[i], ls);
}
hm.get(words[i]).add(i);
}
} public int shortest(String word1, String word2) {
List<Integer> l1 = hm.get(word1);
List<Integer> l2 = hm.get(word2);
int res = Integer.MAX_VALUE;
int i = 0;
int j = 0;
while(i<l1.size() && j<l2.size()){
int index1 = l1.get(i);
int index2 = l2.get(j);
if(index1<index2){
res = Math.min(res, index2-index1);
i++;
}else{
res = Math.min(res, index1-index2);
j++;
}
}
return res;
}
} // Your WordDistance object will be instantiated and called as such:
// WordDistance wordDistance = new WordDistance(words);
// wordDistance.shortest("word1", "word2");
// wordDistance.shortest("anotherWord1", "anotherWord2");

类似Shortest Word Distance.

跟上Shortest Word Distance III.

LeetCode Shortest Word Distance II的更多相关文章

  1. [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 ...

  2. [LeetCode] Shortest Word Distance I & II & III

    Shortest Word Distance Given a list of words and two words word1 and word2, return the shortest dist ...

  3. [LeetCode] Shortest Word Distance III 最短单词距离之三

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

  4. [LeetCode] Shortest Word Distance 最短单词距离

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  5. LeetCode Shortest Word Distance

    原题链接在这里:https://leetcode.com/problems/shortest-word-distance/ 题目: Given a list of words and two word ...

  6. [leetcode]244. Shortest Word Distance II最短单词距离(允许连环call)

    Design a class which receives a list of words in the constructor, and implements a method that takes ...

  7. [LeetCode] 244. Shortest Word Distance II 最短单词距离 II

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

  8. [LeetCode#244] Shortest Word Distance II

    Problem: This is a follow up of Shortest Word Distance. The only difference is now you are given the ...

  9. LeetCode Shortest Word Distance III

    原题链接在这里:https://leetcode.com/problems/shortest-word-distance-iii/ 题目: This is a follow up of Shortes ...

随机推荐

  1. Update From 用法

    今天遇到用一个表的字段填充另一个表的问题,整理了一下   1.在mysql中,应该使用inner join,即: UPDATE   a INNER JOIN b ON a.userName = b.u ...

  2. Write cv::Mat to a file

    如果我们想把OpenCV中的矩阵数据类型cv::Mat保存在一个文件中,可以使用如下的代码: void writeMatToFile(cv::Mat& m, const char* filen ...

  3. 获取当前 Windows 的安装序列号

    Dim s s = InputBox("当前Windows系统序列号为:", "Windows序列号", GetWindowsSN) WScript.Quit ...

  4. 对Get-Content参数-readcount的解释

    绝大多数用户更关心最新的日志,下面给出一个简单的例子演示从一个文本日志中获取最后的某几行文本行:   # 显示windowsupdate.log 文件的最新5行日志 $logs = Get-Conte ...

  5. Hadoop及子项目备注

    Hadoop CommonHadoop体系最底层的一个模块,为Hadoop各子项目提供各种工具,如:配置文件和日志操作等. AvroAvro是doug cutting主持的RPC项目,有点类似Goog ...

  6. Codeforces Round #375 (Div. 2)——D. Lakes in Berland(DFS连通块)

    D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. QQ、淘宝、阿里旺旺在线网页链接代码及详解 很实用

    你可直接到官网去生成代码,简单.方便,相信都能上网的你,对这不会有难度的,认识字的就行,赶紧去吧!   1.阿里旺旺官网: http://page.1688.com/html/wangwang/dow ...

  8. PHP $_SERVER 详解

    元素/代码 描述 $_SERVER['PHP_SELF'] 当前执行脚本的文件名,与 document root 有关. $_SERVER['GATEWAY_INTERFACE'] 服务器使用的 CG ...

  9. SVN客户端常用命令

    1. 将文件checkout到本地目录 svn checkout path(path是服务器上的目录) 例如: cd /home/www  #进入准备获取的项目路径 svn checkout svn: ...

  10. MZhong's Cover Letter

    Application for front-end developer position in Chicago Office Dear HR, I am writing to apply for th ...