题目地址: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:

  1. Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
  2. Input: word1 = coding”, word2 = practice
  3. Output: 3
  4. Input: word1 = "makes", word2 = "coding"
  5. 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++代码如下:

  1. class Solution {
  2. public:
  3. int shortestDistance(vector<string>& words, string word1, string word2) {
  4. unordered_map<string, vector<int>> position;
  5. for (int i = 0; i < words.size(); ++i) {
  6. position[words[i]].push_back(i);
  7. }
  8. int res = INT_MAX;
  9. for (int i : position[word1]) {
  10. for (int j : position[word2]) {
  11. res = min(res, i < j ? j - i : i - j);
  12. }
  13. }
  14. return res;
  15. }
  16. };

日期

2019 年 9 月 18 日 —— 今日又是九一八

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

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

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

  2. LeetCode 243. Shortest Word Distance (最短单词距离)$

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

  3. [leetcode]243. Shortest Word Distance最短单词距离

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

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

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

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

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

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

  9. 243. Shortest Word Distance 最短的单词index之差

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

随机推荐

  1. 体积与边精确积分DGM方法

    Triangular DGM 1. Basis functions decomposing the domain \(\Omega\) into \(N_e\) conforming non-over ...

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

  3. 痞子衡嵌入式:利用GPIO模块来测量i.MXRT1xxx的系统中断延迟时间

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是i.MXRT1xxx的系统中断延迟时间. 在 <Cortex-M系统中断延迟及其测量方法简介> 一文里,痞子衡介绍了 Cor ...

  4. 日常Java测试第一段 2021/11/12

    课堂测试一 package word_show;import java.io.BufferedReader;import java.io.FileNotFoundException;import ja ...

  5. 【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 ...

  6. 容器之分类与各种测试(三)——list部分用法

    list是一个双向链表 例程 #include<stdexcept> #include<memory.h> #include<string> #include< ...

  7. 顺序栈(C++)

    栈的定义为只允许在表的末端进行插入和删除的线性表.简而言之就是先进后出的线性表. 插入和删除的一端被称呼为栈顶(top),而不允许插入删除的一端被称为栈底(bottom).无元素时的栈即为空栈. 使用 ...

  8. 【Android】No Android SDK found(mac)+ 真机调试

     [1]No Android SDK found 如果没下载SDK,可以去google官方下载 如果因为上网问题,这里提供两个网址,有人整理好了,这里先谢谢他们,下面两个择其一下载 http://to ...

  9. 生成接口文档并同步到postman

    前言 当我们开发需要测试接口时,会遇到以下几个问题 1.如果接口过多,参数过多,一个个参数复制到postman简直能要了我的狗命,重复劳动过多. 2.如果接口过多,参数过多,编写接口文档给测试人员或者 ...

  10. Oracle中的instr函数

    最近修改某个条件,由原来输入一个数据修改为可以输入多个,如图所示: 在实现时用到了regexp_substr函数进行分割连接起来的数据,查询时还用到了instr函数进行判断,但出现了问题,当子库存输入 ...