zoukankan      html  css  js  c++  java
  • [LeetCode] Shortest Word Distance I & II & III

    Shortest Word Distance

    Given a list of words and two words word1 and word2, 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.

     1 class Solution {
     2 public:
     3     int shortestDistance(vector<string>& words, string word1, string word2) {
     4         int idx1 = -1, idx2 = -1, res = words.size();
     5         for (int i = 0; i < words.size(); ++i) {
     6             if (words[i] == word1) {
     7                 idx1 = i;
     8                 if (idx2 != -1) res = min(res, idx1 - idx2); 
     9             } else if (words[i] == word2) {
    10                 idx2 = i;
    11                 if (idx1 != -1) res = min(res, idx2 - idx1);
    12             }
    13         }
    14         return res;
    15     }
    16 };

    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.

     1 class WordDistance {
     2 private:
     3     unordered_map<string, vector<int>> wordidx;
     4 public:
     5     WordDistance(vector<string>& words) {
     6         int n = words.size();
     7         for (int i = 0; i < n; ++i) wordidx[words[i]].push_back(i);
     8     }
     9 
    10     int shortest(string word1, string word2) {
    11         vector<int> &idx1 = wordidx[word1];
    12         vector<int> &idx2 = wordidx[word2];
    13         int m = idx1.size(), n = idx2.size();
    14         int res = INT_MAX, i = 0, j = 0;
    15         while (i < m && j < n) {
    16             res = min(res, abs(idx1[i] - idx2[j]));
    17             if (idx1[i] > idx2[j]) ++j;
    18             else ++i;
    19         }
    20         return res;
    21     }
    22 };
    23 
    24 
    25 // Your WordDistance object will be instantiated and called as such:
    26 // WordDistance wordDistance(words);
    27 // wordDistance.shortest("word1", "word2");
    28 // wordDistance.shortest("anotherWord1", "anotherWord2");

    Shortest Word Distance III

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

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

    word1 and word2 may be the same and they represent two individual words in the list.

    For example,
    Assume that words = ["practice", "makes", "perfect", "coding", "makes"].

    Given word1 = “makes”word2 = “coding”, return 1.
    Given word1 = "makes"word2 = "makes", return 3.

    Note:
    You may assume word1 and word2 are both in the list.

     1 class Solution {
     2 public:
     3     int shortest(vector<string> &words, string word) {
     4         int pre = -1, res = INT_MAX;
     5         int n = words.size();
     6         for (int i = 0; i < n; ++i) {
     7             if (words[i] == word) {
     8                 if (pre != -1) res = min(res, i - pre);
     9                 pre = i;
    10             }
    11         }
    12         return res;
    13     }
    14     int shortestWordDistance(vector<string>& words, string word1, string word2) {
    15         if (word1 == word2) return shortest(words, word1);
    16         int idx1 = -1, idx2 = -1, res = INT_MAX;
    17         int n = words.size();
    18         for (int i = 0; i < n; ++i) {
    19             if (words[i] == word1) {
    20                 idx1 = i;
    21                 if (idx2 != -1) res = min(res, idx1 - idx2);
    22             } else if (words[i] == word2) {
    23                 idx2 = i;
    24                 if (idx1 != -1) res = min(res, idx2 - idx1);
    25             }
    26         }
    27         return res;
    28     }
    29 };
  • 相关阅读:
    接口的显示实现和隐式实现
    Math.Round和四舍五入
    经典SQL语句大全(转)
    简明添加log4net到项目中
    NAnt学习笔记(3) Properties, Loggers & Listeners
    (转)Groupon前传:从10个月的失败作品修改,1个月找到成功
    Pyramid中如何配置多种URL匹配同一个View
    《IT项目管理》读书笔记(4) —— 项目范围管理
    C#语法糖
    枚举类型转换成字符串
  • 原文地址:https://www.cnblogs.com/easonliu/p/4784826.html
Copyright © 2011-2022 走看看