[LC] 243. 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.
Solution1:
O(N^2)
class Solution {
public int shortestDistance(String[] words, String word1, String word2) {
int res = words.length;
for (int i = 0; i < words.length; i++) {
if (words[i].equals(word1)) {
for (int j = 0; j < words.length; j++) {
if (words[j].equals(word2)) {
res = Math.min(res, Math.abs(i - j));
}
}
}
}
return res;
}
}
Solution2:
O(N)
class Solution {
public int shortestDistance(String[] words, String word1, String word2) {
int res = words.length;
int s1 = -1, s2 = -1;
for (int i = 0; i < words.length; i++) {
String cur = words[i];
if (cur.equals(word1)) {
s1 = i;
} else if (cur.equals(word2)) {
s2 = i;
}
if (s1 != -1 && s2 != -1) {
res = Math.min(res, Math.abs(s1 - s2));
}
}
return res;
}
}
[LC] 243. Shortest Word Distance的更多相关文章
- 243. Shortest Word Distance 最短的单词index之差
[抄题]: Given a list of words and two words word1 and word2, return the shortest distance between thes ...
- [LeetCode] 243. Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- 243. Shortest Word Distance
题目: Given a list of words and two words word1 and word2, return the shortest distance between these ...
- 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 ...
- 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 ...
- 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 ...
- [LC] 244. Shortest Word Distance II
Design a class which receives a list of words in the constructor, and implements a method that takes ...
- 【LeetCode】243. Shortest Word Distance 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...
随机推荐
- u-boot中filesize环境变量【转载】
转载地址:https://blog.csdn.net/fzs333/article/details/48518559 U-Boot中的环境命令可以使用$(filesize)来确定刚下载(传输)得到的文 ...
- 关于wireshark的使用
1.简介 Wireshark(前称Ethereal)是一个网络封包分析软件.网络封包分析软件的功能是撷取网络封包,并尽可能显示出最为详细的网络封包资料.Wireshark使用WinPCAP作为接口,直 ...
- P3810 【模板】三维偏序(陌上花开)(CDQ分治)
题目背景 这是一道模板题 可以使用bitset,CDQ分治,K-DTree等方式解决. 题目描述 有 nn 个元素,第 ii 个元素有 a_iai.b_ibi.c_ici 三个属性,设 f(i) ...
- 05 SpringMVC:02.参数绑定及自定义类型转换&&04.SpringMVC返回值类型及响应数据类型&&05.文件上传&&06.异常处理及拦截器
springMVC共三天 第一天: 01.SpringMVC概述及入门案例 02.参数绑定及自定义类型转换 03.SpringMVC常用注解 第二天: 04.SpringMVC返回值类型及响应数据类型 ...
- 题解 P4171 【[JSOI2010]满汉全席】
什么,tarjan?那是什么? 码量太大,我选择放弃 为什么不用dfs写2-sat呢?他会伤心的说 这题2-sat的过程大佬们已经讲得非常清楚了,我就略微提一下,主要讲dfs的原理 2_sat原理 我 ...
- vscode Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
vscode 连接 mysql 时出现这个错误 alter user 'root'@'localhost' identified with mysql_native_password by 'pass ...
- Python—快速排序算法
# _*_ coding=utf-8 _*_ """ 快速排序: 通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比 另外一部分的所有数据都要小,然后 ...
- 自己做的板子,电脑识别不出下载口(micro-usb),程序无法烧入
前几天画了一块cortex-A5的底板,出现一个问题,就是通过micro-usb与电脑连接之后,电脑无法识别出来,这就导致程序无法通过micro-usb烧入进去,经过仔细检查硬件,发现第一,元器件都没 ...
- Python笔记_第一篇_面向过程_第一部分_9.Ubuntu基础操作
第一部分 Ubuntu简介 Ubuntu(乌班图)是一个机遇Debian的以桌面应用为主的Linux操作系统,据说其名称来自非洲南部祖鲁语或科萨语的“Ubuntu”一词,意思是“人性”.“我的存在 ...
- 163邮箱报错: 535 Error: authentication failed
今天更换新的异常信息发件邮箱,重新申请了一个新邮箱,SMTP功能已经开通,调用java代码报异常,错误信息为:535 Error: authentication failed.经过网上查证,原来新的1 ...