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. Java之创建对象>3.Enforce the singleton property with a private constructor or an enum type

     1. 通过一个公开的字段来获取单例 // Singleton with public final field public class Elvis { public static final Elv ...

  2. Knockoutjs之observable和applyBindings的使用

    observable在Knockoutjs中属于一个核心功能,在做监控数据的改变时,必须要用到Knockoutjs的监控属性——observable. ko.observable()的简单使用 首先来 ...

  3. 使用OpenNI 2获取RGBD摄像头深度信息

    NiViewer 安装好摄像头驱动和OpenNI后,在Tools文件夹中可以找到一个程序NiViewer.NiViewer的一些基本控制方法如下: 1. ESC关闭NiViewer程序 2. 右键可以 ...

  4. 删数问题(NOI94)

    删数问题(NOI94) 输入一个高精度的正整数N,去掉其中任意S个数字后剩下的数字按原左右次序组成一个新的正整数.编程对给定的N和S,寻找一种方案使得剩下的数字组成的新数最小.输出新的正整数.(N不超 ...

  5. Java对象池技术的原理及其实现

    看到一片有关于java 对象基础知识,故转载一下,同时学习一下. 摘 要 本文在分析对象池技术基本原理的基础上,给出了对象池技术的两种实现方式.还指出了使用对象池技术时所应注意的问题. 关键词 对象池 ...

  6. centos6.5搭建redmine3.4

    缺陷管理,对问题的持续跟踪!redmine很棒的基于ruby开发 Redmine部署架构  mysql+nginx+ruby+redmine 3.4.x 部署环境 centos 6.5 x64redm ...

  7. Path画直线与弧线

    代码地址如下:http://www.demodashi.com/demo/14754.html 前言 之前讲过Paint和Canvas的基本使用,今天来介绍下Path的使用 涉及内容有: Path画直 ...

  8. 微信小程序支付源码,后台服务端代码

    作者:尹华南,来自原文地址 微信小程序支付绕坑指南 步骤 A:小程序向服务端发送商品详情.金额.openid B:服务端向微信统一下单 C:服务器收到返回信息二次签名发回给小程序 D:小程序发起支付 ...

  9. 【PHP采集】php采集、[\s\S]的使用、正则获取 换行字符串或html块

    1.如图,我想要获取 红框框中的html内容,但是普通的正则一直获取不到: 2.原因剖析:因为html换行了,所以直接 /<h3 class=\"s_name\"(.+?)& ...

  10. 如何开发一个基于 Docker 的 Python 应用

    前言 Python 家族成员繁多,解决五花八门的业务需求.这里将通过 Python 明星项目 IPython Notebook,使其容器化,让大家掌握基础的 Docker 使用方法. IPython ...