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.
For example,
Assume that words = ["practice", "makes", "perfect", "coding", "makes"]
.
Given word1 = “coding”
, word2 = “practice”
, return 3.
Given word1 = "makes"
, word2 = "coding"
, return 1.
Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
链接: http://leetcode.com/problemset/algorithms/
题解:
找两个单词在数组中的距离。这个题比较没有意义...就跟找两个字母的距离一样。乍看题目还以为是word ladder。设置两个变量,分别代表每个单词的lastIndex,之后就是判断和计算了。也可以用一个变量来记录。
Time Complexity - O(n), Space Complexity - O(1)
public class Solution {
public int shortestDistance(String[] words, String word1, String word2) {
if(words == null || words.length == 0 || word1 == null || word2 == null)
return Integer.MAX_VALUE;
int minDistance = Integer.MAX_VALUE;
int word1Index = -1, word2Index = -2; for(int i = 0; i < words.length; i++) {
if(words[i].equals(word1))
word1Index = i;
if(words[i].equals(word2))
word2Index = i;
if(word1Index >= 0 && word2Index >= 0)
minDistance = Math.min(minDistance, Math.abs(word1Index - word2Index));
} return minDistance;
}
}
二刷:
使用两个变量存下 word1和word2在数组中的位置,然后进行计算。
Java:
Time Complexity - O(n), Space Complexity - O(1)
public class Solution {
public int shortestDistance(String[] words, String word1, String word2) {
if (words == null || words.length < 2 || word1 == null || word2 == null) {
return Integer.MAX_VALUE;
}
int word1Pos = -1, word2Pos = -1, minDistance = Integer.MAX_VALUE;
for (int i = 0; i < words.length; i++) {
String curWord = words[i];
if (curWord.equals(word1) || curWord.equals(word2)) {
if (curWord.equals(word1)) {
word1Pos = i;
}
if (curWord.equals(word2)) {
word2Pos = i;
}
if (word1Pos >= 0 && word2Pos >= 0) {
minDistance = Math.min(minDistance, Math.abs(word1Pos - word2Pos));
}
}
}
return minDistance;
}
}
三刷:
Java:
public class Solution {
public int shortestDistance(String[] words, String word1, String word2) {
int minDist = Integer.MAX_VALUE;
int word1Idx = -1, word2Idx = -1;
for (int i = 0; i < words.length; i++) {
if (words[i].equals(word1)) {
word1Idx = i;
if (word2Idx >= 0) minDist = Math.min(minDist, i - word2Idx);
} else if (words[i].equals(word2)) {
word2Idx = i;
if (word1Idx >= 0) minDist = Math.min(minDist, i - word1Idx);
}
}
return minDist;
}
}
Reference:
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 ...
- 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] 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 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...
- 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最短单词距离(允许连环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 ...
随机推荐
- 每日一“酷”之difflib
介绍:difflib 比较序列.该模块包含一些用来计算和处理序列直接差异的工具.她对于比较文本尤其用,其中包含的函数可以使用多种常用差异格式生成报告. 测试数据(splitlines()按行划分为序列 ...
- WPF 控件DataGrid绑定
WPF 手动绑定 DataGrid 例子:前台:<DataGrid AutoGenerateColumns="False" Name="dataGrid1" ...
- Linux使用标准IO的调用函数,分3种形式实现
/*根据cp命令的格式要求,设计一个类cp的功能程序,要求使用标准IO的调用函数,分3种形式实现,字符,行,块.并注意函数功能的分层*/ #include<stdio.h> #includ ...
- 无法创建链接服务器 "TEST" 的 OLE DB 访问接口 "OraOLEDB.Oracle" 的实例
在使用SQLserver建立远程服务连接Oracle的时候出现先下面错误 出现这个错误,我找到最常见的两个原因 1.注册表 <1>按下WIN+R,打开“运行”窗口,输入“regedit”, ...
- windows32下安装zend framework2
首先安装好php(5.3.3以上).apache和mysql apache 开启mod_rewrite 模块 将所有AllowOverride None设置为AllowOverride FileInf ...
- mac下wget用来仿站
wget -c -r -np -k -L -p http://www.domain.com 参考 http://www.v2ex.com/t/147870
- LR(0)语法分析
# include <stdio.h> # include <string.h> //存储LR(0)分析表 struct node { char ch; int num; }; ...
- Smarty插件简单开发
smarty的插件相当简单,你的网站经常会用到一些特定的Url或Controller或Action的路由地址的动态生成,通过smarty插件方式可以很轻松的实现. 在smarty/plugins目录下 ...
- Eclipse插件安装总结
Eclipse的插件安装分为在线安装和离线安装两类: 1.在线安装(新版本的推荐方式) 最常用和最好用的方式,直接使用Eclipse的Ecliplse Marketplace,搜索你需要的插件,然后点 ...
- myeclipse/eclipse添加Spket插件实现ExtJs4.2/ExtJs3智能提示
前言 感谢luotao,本博客是copy这篇博客的:http://www.cnblogs.com/luotaoyeah/p/3803926.html ,因为太重要了,所以笔者再写一次. 重要说明:ec ...