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.

 class Solution {
public:
int shortestDistance(vector<string>& words, string word1, string word2) {
int idx1 = -, idx2 = -, res = words.size();
for (int i = ; i < words.size(); ++i) {
if (words[i] == word1) {
idx1 = i;
if (idx2 != -) res = min(res, idx1 - idx2);
} else if (words[i] == word2) {
idx2 = i;
if (idx1 != -) res = min(res, idx2 - idx1);
}
}
return res;
}
};

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.

 class WordDistance {
private:
unordered_map<string, vector<int>> wordidx;
public:
WordDistance(vector<string>& words) {
int n = words.size();
for (int i = ; i < n; ++i) wordidx[words[i]].push_back(i);
} int shortest(string word1, string word2) {
vector<int> &idx1 = wordidx[word1];
vector<int> &idx2 = wordidx[word2];
int m = idx1.size(), n = idx2.size();
int res = INT_MAX, i = , j = ;
while (i < m && j < n) {
res = min(res, abs(idx1[i] - idx2[j]));
if (idx1[i] > idx2[j]) ++j;
else ++i;
}
return res;
}
}; // Your WordDistance object will be instantiated and called as such:
// WordDistance wordDistance(words);
// wordDistance.shortest("word1", "word2");
// 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.

 class Solution {
public:
int shortest(vector<string> &words, string word) {
int pre = -, res = INT_MAX;
int n = words.size();
for (int i = ; i < n; ++i) {
if (words[i] == word) {
if (pre != -) res = min(res, i - pre);
pre = i;
}
}
return res;
}
int shortestWordDistance(vector<string>& words, string word1, string word2) {
if (word1 == word2) return shortest(words, word1);
int idx1 = -, idx2 = -, res = INT_MAX;
int n = words.size();
for (int i = ; i < n; ++i) {
if (words[i] == word1) {
idx1 = i;
if (idx2 != -) res = min(res, idx1 - idx2);
} else if (words[i] == word2) {
idx2 = i;
if (idx1 != -) res = min(res, idx2 - idx1);
}
}
return res;
}
};

[LeetCode] Shortest Word Distance I & II & III的更多相关文章

  1. [Locked] Shortest Word Distance I & II & III

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

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

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

  4. LeetCode Shortest Word Distance II

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

  5. LeetCode Shortest Word Distance III

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

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

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

  7. LeetCode Shortest Word Distance

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

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

  9. [LeetCode] 245. Shortest Word Distance III 最短单词距离 III

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

随机推荐

  1. Postgresql学习笔记

    一:数据类型 主要有三大类以及其他一些杂项类型: 数值型.字符型.日期型. 数值型: 名称 描述 存储大小 范围 smallint 存储整数,小范围 2字节 -32768 至 +32767 integ ...

  2. logrotate: 管理日志文件

    Erik Troan提供了一种优秀的工具logrotate,它实现了多种多样的日志管理策略,而且在我们举例的所有发行版本上都是标准应用. logrotate的配置文件由一系列规范组成,它们说明了要管理 ...

  3. Java之创建对象>5.Avoid creating unnecessary objects

    String s = new String("stringette"); // DON'T DO THIS! The improved version is simply the ...

  4. java获取文件流

      CreateTime--2017年9月1日14:49:21 Author:Marydon servlet获取文件流的两种方式 方式一:使用绝对路径(推荐使用) import java.io.Inp ...

  5. V-rep学习笔记:串口操作

    VREP Regular API提供了串口操作的相关函数,可以对串口进行打开.关闭和读写: 下面使用一款淘宝上常见的AHRS(Attitude and heading reference system ...

  6. AJAX的同步返回结果值

    function makeJQGridDataFromList(url) {     var rowData;     var viewPage = 0;     var viewTotal = 0; ...

  7. Dlib三维点云示例

    Dlib三维点云示例 源代码来自Dlib的示例代码http://dlib.net/3d_point_cloud_ex.cpp.html 在windows下需要链接winmm``comctl32``gd ...

  8. 跟我学Shiro---无状态 Web 应用集成

    无状态 Web 应用集成 在一些环境中,可能需要把 Web 应用做成无状态的,即服务器端无状态,就是说服务器端不会存储像会话这种东西,而是每次请求时带上相应的用户名进行登录.如一些 REST 风格的 ...

  9. shell 脚本启动spring boot的jar 包

    #!/bin/bash # kill java进程 java_sso_prod_pid=`ps aux|grep sso.jar|grep -v "grep"|awk '{prin ...

  10. ios判断是否有中文

    //判断是否有中文 -(BOOL)IsChinese:(NSString *)str { ; i< [str length];i++){ int a = [str characterAtInde ...