[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 ...
随机推荐
- Java static的用法以及原理(06)
静态:static 用法:是一个修饰符,用于修饰成员(成员变量,成员函数), 当成员被静态修饰后,就多了一个调用方式,除了可以被对象调用外,还可以直接被类名调:类名.静态成员 类名.静态成员 存在:方 ...
- Python笔记_第四篇_高阶编程_检测_1.对函数进行单元检测
1. 对函数进行单元检测: 单元检测: 作用:用来对一个函数.一个类.一个模块进行正确性校验工作. 结果: * 单元测试通过,说明我们测试函数的功能正确. * 单元测试不通过,说明函数有BUG,要么测 ...
- 寒假day07
今天没写毕设,看了一些公司招聘的信息,刷了一点相关面试题 1.在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这 ...
- Win 10 Ctrl + Space 冲突
1. 说明 在IDE里面Ctrl + space 会与 Windows 输入法相互冲突,并且用Ctrl + Space 切换中英文也很不常用(常用直接shift切换). 2. 操作 控制面板——时钟. ...
- (最全最灵活地)利用Jxl工具包实现Excel表的内容读取 、写入(可向已有表中追加数据)
1.引子 (1)读取 Jxl工具比较强大,可以方便地实现Excel表的读取和写入.另一款工具Poi也具有相似的功能,并且功能更多,运用也相对复杂.Poi读取Excel表内容时,需要先判断其内容格式,如 ...
- Codeforces 1288C - Two Arrays
题目大意: 给定n和m,有两个数组,两个数组的长度都等于m 数组内每个元素都在1到n中 对于两个数组对应的位置i,必须满足a[i]<=b[i] a数组必须是不下降的序列 b数组必须是不上升的序列 ...
- 好看的UI组合,为以后自己写组件库做准备
1. 黑色格子背景 { color: rgb(255, 255, 255); text-shadow: 1px 1px 0 rgba(0,0,0,.3); rgb(62, 64, 74); backg ...
- anaconda学习笔记
anaconda介绍 Anaconda指的是一个开源的Python发行版本,其包含了conda.Python等180多个科学包及其依赖项. Conda是一个开源的包.环境管理器,可以用于在同一个机器上 ...
- 关于tomcat报错记录
启动报错关键信息如下: Caused by: java.lang.IllegalStateException: Unable to complete the scan for annotations ...
- 4)PHP命名规则,传值方式
(1)命名规则: 包括变量名,类名,接口名函数名等等 ①基本规则: 只能使用小写字母,下划线或者数字 数字不能开头 不能跟环境和系统关键字重复(比如,if,else,function) ② 驼峰式 ...