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");
  • 相关阅读:
    文本标记
    第一个HTML文档
    HTML入门
    bootstrap fileinput 文件上传
    DPDK rte_hash 简述
    glib学习笔记-基本知识
    linux常用网络命令
    libevent学习过程
    C语言 singleton模式
    oracle命令行导出、导入dmp文件
  • 原文地址:https://www.cnblogs.com/lishiblog/p/5798926.html
Copyright © 2011-2022 走看看