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. Struts2学习笔记四:深入拦截器

    一:拦截器的工作原理 拦截器的执行过程可以类比filter过滤器,ActionInvocation实例执行过程中,先执行action实例上引用的拦截器们,然后才执行action实例处理请求,返回res ...

  2. Web网站错误提示页面和默认訪问页面设置

    1.asp.net 定制简单的错误处理页面 通常web应用程序在公布后.为了给用户一个友好界面和使用体验,都会在发生错误时跳转至一个自己定义的错误页面,而不是asp.net向用户暴露出来的具体的异常列 ...

  3. python模块之JSON

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python模块之JSON #1.JSON #JSON表示的对象就是标准的JavaScript语言的对象 # ...

  4. java 增量运算符

    //java 增量运算符 public class Test16{ public static void main(String args[]) { int x1=10; x1+=3; //x1=x1 ...

  5. MongoDB安装实录

      mongodb是nosql中的贵族,很受欢迎... 01.下载 官方:https://www.mongodb.com 社区版.企业版 https://www.mongodb.com/downloa ...

  6. 基于SpringSecurity和JWT的用户访问认证和授权

    发布时间:2018-12-03   技术:springsecurity+jwt+java+jpa+mysql+mysql workBench   概述 基于SpringSecurity和JWT的用户访 ...

  7. Spring-security-Oauth2.0

    上周,我想开发OAuth 2.0的一个实例.我检查了Spring-security-Oauth2.0的样例,OAuth 2提供商sparklr2和OAuth 2客户端TONR .我探索在互联网上了一下 ...

  8. 基于RESTful API 怎么设计用户权限控制?

    前言 有人说,每个人都是平等的:也有人说,人生来就是不平等的:在人类社会中,并没有绝对的公平,一件事,并不是所有人都能去做:一样物,并不是所有人都能够拥有.每个人都有自己的角色,每种角色都有对某种资源 ...

  9. 用@resource注解方式完成属性装配

    注入依赖对象可以采用手工装配或自动装配,在实际应用中建议使用手工装配,因为自动装配会产生未知情况,开发人员无法预见最终的装配结果. 1 需要修改xml文件的以下信息.    加入下列红色部分的4行 & ...

  10. 重写Checkbox 改写选择框的大小

    /* 作者:Starts_2000 * 日期:2009-07-30 * 网站:http://www.csharpwin.com CS 程序员之窗. * 你可以免费使用或修改以下代码,但请保留版权信息. ...