【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 ...
随机推荐
- 66-Reorder List
Reorder List My Submissions QuestionEditorial Solution Total Accepted: 64392 Total Submissions: 2818 ...
- 聚合与分组查询,F与Q查询
from django.db.models import Q 查询书籍名称是python入门或者价是555.55的书 book_queryset = models.Book.objects.filte ...
- 一个简单但能考察C语言基础的题目
请看题: #include<stdio.h> int a=1; int main(void) { int a=a; printf("a=d%\n",a); return ...
- MySQL插入大量数据探讨
笔者想进行数据库查询优化探索,但是前提是需要一个很大的表,因此得先导入大量数据至一张表中. 准备工作 准备一张表,id为主键且自增: 方案一 首先我想到的方案就是通过for循环插入 xml文件: &l ...
- linux 实用指令时间日期类
linux 使用指令时间日期类 data 显示当前日期 基本语法 date 显示当前时间 date+%Y 显示当前年份 date+%m 显示当前月份 date+%d 显示当前是哪一天 date &qu ...
- Java偏向锁浅析
偏向锁的定义 顾名思义,偏向锁会偏向第一个访问锁的线程. 如果在接下来的运行过程中,该锁没有被其他线程访问,这持有偏向锁的线程将永远不需要同步 如果在运行过程中,遇到了其他线程抢占锁,则持有偏向锁的线 ...
- Spark(十)【RDD的读取和保存】
目录 一.文件类型 1.Text文件 2.Json文件 3.对象文件 4.Sequence文件 二.文件系统 1. MySQL 2. Hbase 一.文件类型 1.Text文件 读写 读取 scala ...
- [PE]结构分析与代码实现
PE结构浅析 知识导向: 程序最开始是存放在磁盘上的,运行程序首先需要申请4GB的内存,将程序从磁盘copy到内存,但不是直接复制,而是进行拉伸处理. 这也就是为什么会有一个文件中地址和一个Virtu ...
- Android 清除本地缓存
主要功能:清除内.外缓存,清除数据库,清除Sharepreference,清除files和清除自定义目录 public class DataCleanManager { //清除本应用内部缓存(/da ...
- Type difference of character literals in C and C++
Every literal (constant) in C/C++ will have a type information associated with it. In both C and C++ ...