题目地址:https://leetcode-cn.com/problems/shortest-word-distance-iii/

题目描述

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.

Example:

Assume that words = ["practice", "makes", "perfect", "coding", "makes"].

Input: word1 = “makes”, word2 = “coding”
Output: 1
Input: word1 = "makes", word2 = "makes"
Output: 3

Note:

  • You may assume word1 and word2 are both in the list.

题目大意

给定一个单词列表和两个单词 word1 和 word2,返回列表中这两个单词之间的最短距离。

word1 和 word2 是有可能相同的,并且它们将分别表示为列表中两个独立的单词。

解题方法

字典+暴力检索

字典保存每个单词的下标,然后分类讨论:

  1. 如果两个单词不同,那么直接暴力检索这两个的所有坐标的差值绝对值,找最小的。
  2. 如果两个单词相同,那么相当于从一个有序数组中找到相邻数字的最小差值,需要一次遍历。

C++代码如下:

class Solution {
public:
int shortestWordDistance(vector<string>& words, string word1, string word2) {
unordered_map<string, vector<int>> m;
for (int i = 0; i < words.size(); ++i) {
m[words[i]].push_back(i);
}
int res = INT_MAX;
if (word1 != word2) {
for (int i : m[word1]) {
for (int j : m[word2]) {
res = min(res, abs(i - j));
}
}
} else {
for (int i = 1; i < m[word1].size(); ++i) {
res = min(res, m[word1][i] - m[word1][i - 1]);
}
}
return res;
}
};

日期

2019 年 9 月 24 日 —— 梦见回到了小学,小学已经芳草萋萋破败不堪

【LeetCode】245. Shortest Word Distance III 解题报告 (C++)的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 245. Shortest Word Distance III

    题目: This is a follow up of Shortest Word Distance. The only difference is now word1 could be the sam ...

  4. 【LeetCode】244. Shortest Word Distance II 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存出现位置 日期 题目地址:https://le ...

  5. 245. Shortest Word Distance III 单词可以重复的最短单词距离

    [抄题]: Given a list of words and two words word1 and word2, return the shortest distance between thes ...

  6. LC 245. Shortest Word Distance III 【lock, medium】

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  7. [LeetCode] 243. Shortest Word Distance 最短单词距离

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  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]244. Shortest Word Distance II最短单词距离(允许连环call)

    Design a class which receives a list of words in the constructor, and implements a method that takes ...

随机推荐

  1. illumina SNP 芯片转基因型矩阵

    一.芯片数据 此次拿到的illumina芯片数据并不是原始的数据,已经经过GenomeStudio软件处理成了finalreport文件,格式如下: 之前没处理过芯片数据,对于这种编码模式(Forwa ...

  2. 2015百度之星之-IP聚合

    IP聚合  Accepts: 138  Submissions: 293  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 6553 ...

  3. RTSP, RTP, RTCP, RTMP傻傻分不清?

    RTSP基于TCP传输请求和响应报文,RTP基于UDP传输流媒体数据,RTCP基于UDP传送传输质量信息(如丢包和延迟). 比如喀什一个局域网内10个人同时点播广州的同一个源,喀什和广州之间就要传10 ...

  4. day32 HTML

    day32 HTML 什么是前端 只要是跟用户打交道的界面都可以称之为前端 # eg:电脑界面, 手机界面,平板界面, 什么是后端? eg:python, java,php,go, 不跟用户直接打交道 ...

  5. day04:Python学习笔记

    day04:Python学习笔记 1.算数运算符 1.算数运算符 print(10 / 3) #结果带小数 print(10 // 3) #结果取整数,不是四舍五入 print(10 % 3) #结果 ...

  6. nodejs-Cluster模块

    JavaScript 标准参考教程(alpha) 草稿二:Node.js Cluster模块 GitHub TOP Cluster模块 来自<JavaScript 标准参考教程(alpha)&g ...

  7. 30个类手写Spring核心原理之环境准备(1)

    本文节选自<Spring 5核心原理> 1 IDEA集成Lombok插件 1.1 安装插件 IntelliJ IDEA是一款非常优秀的集成开发工具,功能强大,而且插件众多.Lombok是开 ...

  8. oracle extract

    select extract(year from systimestamp) year         ,extract(month from systimestamp) month          ...

  9. JavaIO——转换流、字符编码

    1.转换流 转换流是将字节流变成字符流的流. OutputStreamWriter:将字节输出流转换成字符输出流. public class OutputStreamWriter extends Wr ...

  10. springboot整合jetty

    1.jetty介绍 通常我们进行Java Web项目开发,必须要选择一种服务器来部署并运行Java应用程序,Tomcat和Jetty作为目前全球范围内最著名的两款开源servlet容器,该怎么选呢. ...