原题链接在这里: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.

题解:

类似Shortest Word Distance.

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(1) regardless HashMap.

AC Java:

 1 public class WordDistance {
 2
 3     HashMap<String, List<Integer>> hm;
 4     public WordDistance(String[] words) {
 5         hm = new HashMap<String, List<Integer>>();
 6         for(int i = 0; i<words.length; i++){
 7             if(!hm.containsKey(words[i])){
 8                 List<Integer> ls = new ArrayList<Integer>();
 9                 hm.put(words[i], ls);
10             }
11             hm.get(words[i]).add(i);
12         }
13     }
14
15     public int shortest(String word1, String word2) {
16         List<Integer> l1 = hm.get(word1);
17         List<Integer> l2 = hm.get(word2);
18         int res = Integer.MAX_VALUE;
19         int i = 0;
20         int j = 0;
21         while(i<l1.size() && j<l2.size()){
22             int index1 = l1.get(i);
23             int index2 = l2.get(j);
24             if(index1<index2){
25                 res = Math.min(res, index2-index1);
26                 i++;
27             }else{
28                 res = Math.min(res, index1-index2);
29                 j++;
30             }
31         }
32         return res;
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");

跟上Shortest Word Distance III.

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/5186304.html

LeetCode Shortest Word Distance II相关推荐

  1. 244. Shortest Word Distance II

    因为要多次查询,每次都遍历肯定不行,所以要保存信息. 最直接的就是保存每个string出现的位置,查询A,B的时候就直接比较他们所有的出现INDEX中最小的情况. 有一点需要注意的是,A出现的位置和B ...

  2. 【DFS + 记忆化递归】LeetCode 140. Word Break II

    LeetCode 140. Word Break II Solution1:我的答案 纯DFS,在第31个case时超时,还是记录一下.. class Solution { // DFS public ...

  3. 【DFS + Backtracking】LeetCode 212. Word Search II

    LeetCode 212. Word Search II Solution1:我的答案 暴力搜索..基于第79题的答案,真的是非常之慢啊!!! 快的方法均是基于字典树的方法,真是复杂.. class ...

  4. 【难点+重点BFS】LeetCode 126. Word Ladder II

    LeetCode 126. Word Ladder II Solution1: 参考自花花酱:http://zxi.mytechroad.com/blog/searching/leetcode-126 ...

  5. leetcode 140. Word Break II | 140. 单词拆分 II(动态规划)

    题目 https://leetcode.com/problems/word-break-ii/ 题解 由 leetcode 139. Word Break | 139. 单词拆分(动态规划) 改造而来 ...

  6. leetcode 212. Word Search II | 212. 单词搜索 II(Trie,回溯,DFS)

    题目 https://leetcode.com/problems/word-search-ii/ 题解 基于前缀树实现,如果把 Trie 也看做一个特殊的图的话,就是 将两个图同时进行 dfs,就像判 ...

  7. [Leetcode] 212. Word Search II 解题报告

    题目: Given a 2D board and a list of words from the dictionary, find all words in the board. Each word ...

  8. [LeetCode] Word Break II 拆分词句之二

    [LeetCode] Word Break II 拆分词句之二 Given a string s and a dictionary of words dict, add spaces in s to ...

  9. LeetCode 127. Word Ladder

    原题链接在这里:https://leetcode.com/problems/word-ladder/ 题目: Given two words (beginWord and endWord), and ...

  10. LeetCode 79. Word Search

    原题链接在这里:https://leetcode.com/problems/word-search/ 题目: Given a 2D board and a word, find if the word ...

最新文章

  1. Maptree-层级结构数据展示的绝佳尝试
  2. PHP做下载文件的方法
  3. [LeetCode] Remove Duplicates from Sorted Array II
  4. 张家口以太坊智能合约开发实战pdf_以太坊的再次腾飞,你看得懂么?
  5. 使用SAP Analytics Cloud展示全球新冠肺炎确诊总人数的分布情况
  6. python定义类的程序_python扫码签到程序python中如何定义类
  7. Apache Dubbo是一款高性能Java RPC框架。
  8. Google I/O 2019 行纪 —— Google 要让 AI 消除偏见
  9. 重大发现: windows下C++ UI库 UI神器-SOUI(转载)
  10. 数据中心201812-4
  11. Atom:一些有用的Packages和插件
  12. win7工作组无法查看计算机名,win7系统无法查看工作组计算机怎么解决
  13. 16个博士回河南乡村创业,已有上市计划
  14. java读取xslx内容,内容转换成docx和pdf,包括图片
  15. BELLHOP 关于Actup冲激响应的绘制
  16. html5图片邀请函,html5,邀请函.doc
  17. 欧美软件外包三种模式
  18. 【凸优化】关于 KKT 条件 及其最优性
  19. 数字电路3-8译码器
  20. 分析快、易操作的数据分析工具推荐

热门文章

  1. 她经济正在替代男性,成为体育产业的新支柱?
  2. 阿里云爬虫风险管理产品商业化,为云端流量保驾护航 1
  3. 工业互联网为湖南制造装上“智脑”
  4. 计算机体系结构五大部分组成
  5. videojs--跨浏览器的HTML视频播放器(可自定义样式)
  6. 各种开源协议介绍 BSD、Apache Licence、GPLv2 、v3 、LGPL、MIT
  7. Mybatis破MySql8小时断线问题
  8. POJ-1087 A Plug for UNIX 网络流
  9. 让代码在SharePoint页面执行如何在aspx页面中写代码
  10. [收藏]Web创业的10条戒律