【LeetCode】244. Shortest Word Distance II 解题报告 (C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/shortest-word-distance-ii/
题目描述
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. Your method will be called repeatedly many times with different parameters.
Example:
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
Input: word1 = “coding”, word2 = “practice”
Output: 3
Input: word1 = "makes", word2 = "coding"
Output: 1
Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
题目大意
请设计一个类,使该类的构造函数能够接收一个单词列表。然后再实现一个方法,该方法能够分别接收两个单词 word1 和 word2,并返回列表中这两个单词之间的最短距离。您的方法将被以不同的参数调用 多次。
解题方法
字典保存出现位置
这个题让我们求两个字符串出现的最短距离,其实很好办,先分别找到这两个单词出现的位置,然后两两比较,找出最短距离即可。因为给出的words里面会有重复,所以应该使用unordered_map<string, vector<int>> positions;
保存所有出现的位置。
C++代码如下:
class WordDistance {
public:
WordDistance(vector<string>& words) {
for (int i = 0; i < words.size(); ++i) {
positions[words[i]].push_back(i);
}
}
int shortest(string word1, string word2) {
vector<int> pos1 = positions[word1];
vector<int> pos2 = positions[word2];
int res = INT_MAX;
for (int p1 : pos1) {
for (int p2 : pos2) {
res = min(res, abs(p1 - p2));
}
}
return res;
}
private:
unordered_map<string, vector<int>> positions;
};
/**
* Your WordDistance object will be instantiated and called as such:
* WordDistance* obj = new WordDistance(words);
* int param_1 = obj->shortest(word1,word2);
*/
日期
2019 年 9 月 22 日 —— 熬夜废掉半条命
【LeetCode】244. Shortest Word Distance II 解题报告 (C++)的更多相关文章
- [leetcode]244. Shortest Word Distance II最短单词距离(允许连环call)
Design a class which receives a list of words in the constructor, and implements a method that takes ...
- [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 ...
- [LeetCode#244] Shortest Word Distance II
Problem: This is a follow up of Shortest Word Distance. The only difference is now you are given the ...
- 244. Shortest Word Distance II
题目: This is a follow up of Shortest Word Distance. The only difference is now you are given the list ...
- 【LeetCode】245. Shortest Word Distance III 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+暴力检索 日期 题目地址:https://lee ...
- 【LeetCode】212. Word Search II 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前缀树 日期 题目地址:https://leetco ...
- 【LeetCode】140. Word Break II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...
- 244. Shortest Word Distance II 实现数组中的最短距离单词
[抄题]: Design a class which receives a list of words in the constructor, and implements a method that ...
- LC 244. Shortest Word Distance II 【lock, Medium】
Design a class which receives a list of words in the constructor, and implements a method that takes ...
随机推荐
- PicGo + Gitee +Typora实现markdown图床
目录 1. PicGo安装 2.Gitee配置 3.配置PicGo 3.Typora的设置 网上有一些很详细的教程,我这里只记录要点,其余部分按以下教程步骤来就行. 1. PicGo安装 国内下载可能 ...
- Xshell初步设置
目录 双击复制,右击粘贴 双击复制全路径 复制窗口:双击窗口 编码:设置utf-8 外观设置: 窗口化文件传输 vim中使用鼠标点击移动 隐藏/出现菜单栏 ctrl+鼠标控制字体大小 alt+O 弹出 ...
- kubernetes 用到的工具及组件
kubernetes 用到的工具及组件,将所有的组件下载后放到/usr/local/bin目录下(记得chmod a+x /usr/local/bin/*).所有的组件,原则上都用最新的,如果遇到不支 ...
- 联盛德 HLK-W806 (六): I2C驱动SSD1306 128x64 OLED液晶屏
目录 联盛德 HLK-W806 (一): Ubuntu20.04下的开发环境配置, 编译和烧录说明 联盛德 HLK-W806 (二): Win10下的开发环境配置, 编译和烧录说明 联盛德 HLK-W ...
- linux 软链接与查看历史指令
ln 说明 软连接也叫符号链接,类似于windows里的快捷方式,主要存放了路径. 基本语法 ln -s[原文件或目录][软连接名] 删除软链接 [root@hadoop102 ~]# rm -rf ...
- VSCode+Maven+Hadoop开发环境搭建
在Maven插件的帮助下,VSCode写Java其实非常方便.这一讲我们介绍如何借助maven用VScode搭建Hadoop开发环境. 1.Java环境安装 首先我们需要搭建好Java开发环境.我们需 ...
- Apache架构师的30条设计原则
本文作者叫 Srinath,是一位科学家,软件架构师,也是一名在分布式系统上工作的程序员. 他是 Apache Axis2 项目的联合创始人,也是 Apache Software 基金会的成员. 他是 ...
- JavaIO——内存操作流、打印流
我们之前所做的都是对文件进行IO处理,实则我们也可以对内存进行IO处理.我们将发生在内存中的IO处理称为内存流. 内存操作流也可分为两类:字节内存流和字符内存流. (1)ByteArrayInputS ...
- Springboot Oauth2 集成Swagger2权限验证实战
Swagger是什么?能干什么?在这就不展开讲解了.本文主要讲解如何集成OAuth2的Password模式权限验证,验证接口是否具有权限. 引入依赖 <dependency> <gr ...
- 【Linux】【Commands】文件管理工具
文件管理工具:cp, mv, rm cp命令:copy 源文件:目标文件 单源复制:cp [OPTION]... [-T] SOURCE DEST 多源复制:cp [OPTION]... SOURCE ...