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.
分析:
因为word1或者word2在数组里可能有重复,所以,每个词可能会出现在不同的地方。用ArrayList记录word1(word2)出现的index, 然后找到最近的indexs。
考虑到后续问题是如果那个method多被多次call,我们可以用hashmap记录每个word出现的index.
public class WordDistance {
private Map<String, List<Integer>> map;
public WordDistance(String[] words) {
map = new HashMap<String, ArrayList<Integer>>();
for (int i = ; i < words.length; i++) {
if (map.containsKey(words[i])) {
List<Integer> pos = map.get(words[i]);
pos.add(i);
map.put(words[i], pos);
} else {
List<Integer> pos = new ArrayList<>();
pos.add(i);
map.put(words[i], pos);
}
}
}
public int shortest(String word1, String word2) {
List<Integer> pos1 = map.get(word1);
List<Integer> pos2 = map.get(word2);
int minDistance = Integer.MAX_VALUE;
int i = ;
int j = ;
while (i < pos1.size() && j < pos2.size()) {
int p1 = pos1.get(i);
int p2 = pos2.get(j);
if (p1 < p2) {
minDistance = Math.min(minDistance, p2 - p1);
i++;
} else {
minDistance = Math.min(minDistance, p1 - p2);
j++;
}
}
return minDistance;
}
}
如果两个单词不一样,而且只被call一次,那么下面这个方法也不错。
public class Solution {
public int shortestDistance(String[] words, String word1, String word2) {
int index1 = -;
int index2 = -;
int min = Integer.MAX_VALUE;
for(int i = ; i < words.length; i++){
if(words[i].equals(word1)){
index1 = i;
}else if(words[i].equals(word2)){
index2 = i;
}
if(index1 != - && index2 != -){
min = Math.min(min, Math.abs(index1 - index2));
}
}
return min;
}
}
Shortest Word Distance的更多相关文章
- [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 II 最短单词距离之二
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...
- [LeetCode] Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- LeetCode Shortest Word Distance III
原题链接在这里:https://leetcode.com/problems/shortest-word-distance-iii/ 题目: This is a follow up of Shortes ...
- LeetCode Shortest Word Distance II
原题链接在这里:https://leetcode.com/problems/shortest-word-distance-ii/ 题目: This is a follow up of Shortest ...
- LeetCode Shortest Word Distance
原题链接在这里:https://leetcode.com/problems/shortest-word-distance/ 题目: Given a list of words and two word ...
- 245. Shortest Word Distance III
题目: This is a follow up of Shortest Word Distance. The only difference is now word1 could be the sam ...
- 244. Shortest Word Distance II
题目: This is a follow up of Shortest Word Distance. The only difference is now you are given the list ...
- [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 ...
- [Locked] Shortest Word Distance I & II & III
Shortest Word Distance Given a list of words and two words word1 and word2, return the shortest dist ...
随机推荐
- iOS边练边学--多线程介绍、NSThread的简单实用、线程安全以及线程之间的通信
一.iOS中的多线程 多线程的原理(之前多线程这块没好好学,之前对多线程的理解也是错误的,这里更正,好好学习这块) iOS中多线程的实现方案有以下几种 二.NSThread线程类的简单实用(直接上代码 ...
- Eclipse_调试技巧
一.使用Display视图实时计算变量结果(带智能提示) windows-->show view-->display http://stackoverflow.com/questions ...
- Java基础-静态代理与动态代理比较
JAVA的静态代理与动态代理比较 静态代理类: 由程序员创建或由特定工具自动生成源代码,再对其编译.在程序运行前,代理类的.class文件就已经存在了.动态代理类: 在程序运行时,运用反射机制动态创建 ...
- 获取和设置tinyMCE 4编辑器的内容
对于tinymce编辑器是无法通过js进行内容的读写的,必须使用编辑器自身的方法才行,下面是一些方法,希望能对用到的朋友有所帮助: 1.如果当前页面只有一个编辑器: 获取内容:tinyMCE.acti ...
- python 类型之 set
python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和 ...
- MyEclipse护眼模式、字体大小的调整
1.Eclipse改变背景颜色 Windows menu --> Preference General -> Editors -> Text Editors(click), 在底部 ...
- PHP配置,php.ini以及覆盖问题
在部署一个cms项目到服务器上的时候,因为cms的模板比较老,服务器上用的php是5.3.3版(大于5.3,可以认为是新的),有些页面会显示"deprecated"类别的错误信息. ...
- what linux java cpu 100% ?
1.用top找到最耗资源的进程id [ bin]# toptop - 16:56:14 up 119 days, 6:17, 7 users, load average: 2.04, 2.07, 2. ...
- BUAA1389愤怒的DZY(最大值最小化)
http://acm.buaa.edu.cn/problem/1389/ 愤怒的DZY[问题描述]“愤怒的小鸟”如今已经是家喻户晓的游戏了,机智的WJC最近发明了一个类似的新游戏:“愤怒的DZY”.游 ...
- HD1532Drainage Ditches(最大流模板裸题 + 邻接表)
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...