zoukankan      html  css  js  c++  java
  • 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.

    Solution:

    Use HashMap to store the indexs of each word.

     1 public class WordDistance {
     2     Map<String, List<Integer>> wordMap;
     3 
     4     public WordDistance(String[] words) {
     5         wordMap = new HashMap<String, List<Integer>>();
     6         for (int i = 0; i < words.length; i++) {
     7             String word = words[i];
     8             if (wordMap.containsKey(word)) {
     9                 wordMap.get(word).add(i);
    10             } else {
    11                 List<Integer> list = new ArrayList<Integer>();
    12                 list.add(i);
    13                 wordMap.put(word, list);
    14             }
    15         }
    16     }
    17 
    18     public int shortest(String word1, String word2) {
    19         // Get two index lists
    20         List<Integer> indList1 = wordMap.get(word1);
    21         List<Integer> indList2 = wordMap.get(word2);
    22         int p1 = 0, p2 = 0;
    23         int minDis = Integer.MAX_VALUE;
    24         while (true) {
    25             if (indList1.get(p1) < indList2.get(p2)) {
    26                 // Move p1.
    27                 while (p1 < indList1.size() && indList1.get(p1) < indList2.get(p2)) {
    28                     minDis = Math.min(minDis, Math.abs(indList1.get(p1) - indList2.get(p2)));
    29                     p1++;
    30                 }
    31                 if (p1 >= indList1.size())
    32                     break;
    33             } else {
    34                 // Move p2.
    35                 while (p2 < indList2.size() && indList2.get(p2) < indList1.get(p1)) {
    36                     minDis = Math.min(minDis, Math.abs(indList1.get(p1) - indList2.get(p2)));
    37                     p2++;
    38                 }
    39                 if (p2 >= indList2.size())
    40                     break;
    41             }
    42         }
    43         return minDis;
    44     }
    45 }
    46 
    47 
    48 // Your WordDistance object will be instantiated and called as such:
    49 // WordDistance wordDistance = new WordDistance(words);
    50 // wordDistance.shortest("word1", "word2");
    51 // wordDistance.shortest("anotherWord1", "anotherWord2");
  • 相关阅读:
    VSTO不能创建OFFICE 文档项目的原因
    vs2016 创建 vsto excel 文件项目的一个问题
    一个开发原则:永远不要返回NULL
    客户为什么习惯变更需求
    从实际项目中的一个改进细节谈程序的易用性优化
    第三方系统打开EAFC的实现
    功能间(两个form)数据交互的编程方法
    关于行军模式大批量数据的审批的实现
    程序的升级发布管理
    转:从如何判断浮点数是否等于0说起——浮点数的机器级表示 献给依然 if ( double i ==0.00)的菜鸟们
  • 原文地址:https://www.cnblogs.com/lishiblog/p/5798926.html
Copyright © 2011-2022 走看看