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 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.
这题虽然可以沿用上一题的思路,但是可以有更简单的两个index的做法,最优解O(n),runtime 8ms。
class Solution {
public:
int shortestWordDistance(vector<string>& words, string word1, string word2) {
int index = -;
int minval = words.size();
for(int i=; i<words.size(); i++){
if((index != -) && (words[i] == word1 || words[i] == word2)){
if((words[index] == word1 && words[i] == word2) ||
(words[index] == word2 && words[i] == word1)){
minval = min(minval, i - index);
}
}
if(words[i] == word1 || words[i] == word2)
index = i;
}
return minval;
}
};
LC 245. Shortest Word Distance III 【lock, medium】的更多相关文章
- LC 244. Shortest Word Distance II 【lock, Medium】
Design a class which receives a list of words in the constructor, and implements a method that takes ...
- [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 ...
- 245. Shortest Word Distance III
题目: This is a follow up of Shortest Word Distance. The only difference is now word1 could be the sam ...
- 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】245. Shortest Word Distance III 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+暴力检索 日期 题目地址:https://lee ...
- 245. Shortest Word Distance III 单词可以重复的最短单词距离
[抄题]: Given a list of words and two words word1 and word2, return the shortest distance between thes ...
- [LeetCode] 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 Shortest Word Distance III
原题链接在这里:https://leetcode.com/problems/shortest-word-distance-iii/ 题目: This is a follow up of Shortes ...
- [Swift]LeetCode245.最短单词距离 III $ Shortest Word Distance III
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...
随机推荐
- PCB拼板
- Vue快速学习_第四节
获取原生的DOM方式($.refs) 给标签或者组件 添加ref <div ref = 'liu'>test</div> <Home ref = 'home'>&l ...
- Selenium(1)
一.Selenium->Se(硒)->功能自动化测试工具=功能自动化测试工具(QTP)<-Mercury(汞) 1.Selenium介绍 (1)Selenium 是针对web被测系统 ...
- SPOJ 1825 经过不超过K个黑点的树上最长路径 点分治
每一次枚举到重心 按子树中的黑点数SORT一下 启发式合并 #include<cstdio> #include<cstring> #include<algorithm&g ...
- POJ 1741 单次询问树上距离<=K的点对数 点分治
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; ; ; ], ...
- C#工具:ASP.net 调用SQLserver帮助类
一.正常调用 1.创建DBHelper帮助类 2.复制以下代码到类中 using System; using System.Collections.Generic; using System.Data ...
- 使用SetWindowHookEx注入global hook
写下这是为了自己复习的. 主要实现的是给File Explorer注入鼠标钩子,以检测鼠标是否在File Explorer上点击 .cpp #include <Windows.h> #in ...
- MySQL字符集或字符序
字符集基础 字符集:数据库中的字符集包含两层含义 各种文字和符号的集合,包括各国家文字,标点符号,图形符号,数字等. 字符的编码方式,即二进制数据与字符的映射规则: 字符集分类: ASCI ...
- 多组件共享-vuex —— 使用vuex 报错 actions should be function or object with ”handler“
vuex分模块使用时出现的问题,单文件暂时没有用到 原因是在action 文件中没有任何定义(即:文件为空)或则 action 没有任何方法返回,将action在模块引用时去掉即可 转自:https: ...
- c语言结构体以及结构体指针的使用
结构体: 正常定义一个结构体: typedef struct node{ ]; int len; }Seq,*llink; 定义结构体指针: Seq *s;或者llink t;之后关于s与t的用法,其 ...