【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 ...
随机推荐
- DNS域名解析全过程
一张图看懂DNS域名解析全过程 DNS域名解析是互联网上非常重要的一项服务,上网冲浪(还有人在用这个词吗?)伴随着大量DNS服务来支撑,而对于网站运营来说,DNS域名解析的稳定可靠,意味着更多用户 ...
- Python中pymysql基本使用
Python中pymysql模块通过获取mysql数据库命令行游标执行数据库命令来进行数据库操作 优点:操作数据库语句所见即所得,执行了什么数据库语句都很清楚 缺点:操作繁琐,代码量多 1. pymy ...
- Linux之sed命令常见用法
1. sed(stream editor),流编辑器 linux中,主要中sed命令实现对文件的增删改替换查 名称 sed - 用于过滤和转换文本的流编辑器 SYNOPSIS sed [选项]... ...
- gcc 的编译流程 和gdb的调试方法
GCC的编译流程分为四个步骤: 预处理(Pre-Processing) 编译(Compiling) 汇编(Assembling) 链接(Linking) 可以看的出来文件大小 gdb 调试 gdb - ...
- Label -- 跳出循环的思路
let num = 0 ; outPoint: //label for (let i = 0; i < 10; i++) { for ( let j = 0; j < 10; j++) { ...
- 生产调优2 HDFS-集群压测
目录 2 HDFS-集群压测 2.1 测试HDFS写性能 测试1 限制网络 1 向HDFS集群写10个128M的文件 测试结果分析 测试2 不限制网络 1 向HDFS集群写10个128M的文件 2 测 ...
- Scala(四)【集合基础入门】
目录 一.Array 二. List 三.Set 四.Tuple 五.Map 一.Array package com.bigdata.scala.day01 /** * @description: 不 ...
- nodejs-CommonJS规范
JavaScript 标准参考教程(alpha) 草稿二:Node.js CommonJS规范 GitHub TOP CommonJS规范 来自<JavaScript 标准参考教程(alpha) ...
- C语言time函数获取当前时间
以前放了个链接,但是原作者把博文删了,这里放一个获取时间的代码,已经比较详细所以不做注释 #include<stdio.h> #include<time.h> #include ...
- 【Linux】【Database】【MySQL】使用percona搭建高可用的MySQL数据库
1. 简介 1.1. 官方文档: 数据库架构:https://docs.openstack.org/ha-guide/shared-database.html 1.2. 本次使用的的是Percona ...