【LeetCode】243. Shortest Word Distance 解题报告(C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/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.
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,返回列表中这两个单词之间的最短距离。
解题方法
字典
字典保存每个单词出现位置,然后对于word1和word2的位置两两交叉求最小值即可。
C++代码如下:
class Solution {
public:
int shortestDistance(vector<string>& words, string word1, string word2) {
unordered_map<string, vector<int>> position;
for (int i = 0; i < words.size(); ++i) {
position[words[i]].push_back(i);
}
int res = INT_MAX;
for (int i : position[word1]) {
for (int j : position[word2]) {
res = min(res, i < j ? j - i : i - j);
}
}
return res;
}
};
日期
2019 年 9 月 18 日 —— 今日又是九一八
【LeetCode】243. Shortest Word Distance 解题报告(C++)的更多相关文章
- [LeetCode] 243. Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- LeetCode 243. Shortest Word Distance (最短单词距离)$
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- [leetcode]243. Shortest Word Distance最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- [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] 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 ...
- LeetCode 245. Shortest Word Distance III (最短单词距离之三) $
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...
- [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 ...
- 243. Shortest Word Distance 最短的单词index之差
[抄题]: Given a list of words and two words word1 and word2, return the shortest distance between thes ...
随机推荐
- 体积与边精确积分DGM方法
Triangular DGM 1. Basis functions decomposing the domain \(\Omega\) into \(N_e\) conforming non-over ...
- 解决UE4项目编辑器启动时出现LogLinker: Warning: Failed to load '/Game/FirstPersonBP/FirstPersonOverview': Can't find file.
UE4版本:4.24.3源码编译版本 Windows10 + VS2019环境 LogLinker: Warning: Failed to load '/Game/FirstPersonBP/Firs ...
- 痞子衡嵌入式:利用GPIO模块来测量i.MXRT1xxx的系统中断延迟时间
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是i.MXRT1xxx的系统中断延迟时间. 在 <Cortex-M系统中断延迟及其测量方法简介> 一文里,痞子衡介绍了 Cor ...
- 日常Java测试第一段 2021/11/12
课堂测试一 package word_show;import java.io.BufferedReader;import java.io.FileNotFoundException;import ja ...
- 【leetcode】598. Range Addition II
You are given an m x n matrix M initialized with all 0's and an array of operations ops, where ops[i ...
- 容器之分类与各种测试(三)——list部分用法
list是一个双向链表 例程 #include<stdexcept> #include<memory.h> #include<string> #include< ...
- 顺序栈(C++)
栈的定义为只允许在表的末端进行插入和删除的线性表.简而言之就是先进后出的线性表. 插入和删除的一端被称呼为栈顶(top),而不允许插入删除的一端被称为栈底(bottom).无元素时的栈即为空栈. 使用 ...
- 【Android】No Android SDK found(mac)+ 真机调试
[1]No Android SDK found 如果没下载SDK,可以去google官方下载 如果因为上网问题,这里提供两个网址,有人整理好了,这里先谢谢他们,下面两个择其一下载 http://to ...
- 生成接口文档并同步到postman
前言 当我们开发需要测试接口时,会遇到以下几个问题 1.如果接口过多,参数过多,一个个参数复制到postman简直能要了我的狗命,重复劳动过多. 2.如果接口过多,参数过多,编写接口文档给测试人员或者 ...
- Oracle中的instr函数
最近修改某个条件,由原来输入一个数据修改为可以输入多个,如图所示: 在实现时用到了regexp_substr函数进行分割连接起来的数据,查询时还用到了instr函数进行判断,但出现了问题,当子库存输入 ...