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 ...
随机推荐
- reboot-css
dd, label { margin-bottom: .5rem; }abbr[title] { text-decoration: none; }abbr[title] { border-bottom ...
- hdu4333 扩展KMP
慢慢研究可以发现,可以用扩展kmp来求.由于扩展kmp的next[]只有一部分,当前位子前面那部分和母串的后部分,所以可以将字符串复制接在后面一次. 先求如果next[]>0&& ...
- Html-input文本框只能输入数字
onKeyPress="if ((event.keyCode < 48 || event.keyCode > 57)) event.returnValue = false;&qu ...
- Java基础-final变量和普通变量的区别
当用final作用于类的成员变量时,成员变量(注意是类的成员变量,局部变量只需要保证在使用之前被初始化赋值即可)必须在定义时或者构造器中进行初始化赋值,而且final变量一旦被初始化赋值之后,就不能再 ...
- Learn sed using these command on Linux(流线式编辑器——sed)
是对文件中的每一行进行处理,不会对源文件进行修改 sed --version sed '11d' sed_file sed -n '/[Bb]erry/p' sed_file (由于设置了n,所以只打 ...
- Codevs2157 配对
题目描述 Description 给出2个序列A={a[1],a[2],…,a[n]},B={b[1],b[2],…,b[n]},从A.B中各选出n个元素进行一一配对(可以不按照原来在序列中的顺序), ...
- linux下svn命令大全
linux下svn命令大全 1.将文件checkout到本地目录 svn checkout path(path是服务器上的目录) 例如:svn checkout svn://192.168.1.1/p ...
- LESSCSS的几点摘要
字符串插值 变量可以用像 @{name} 这样的结构,以类似 ruby 和 php 的方式嵌入到字符串中: @base-url: "http://assets.fnord.com" ...
- tcp 重发 应用层重传
采用TCP时,应用层需要超时重传吗? 需要,原因如下: 1 tcp的超时控制不是你能设置的,所有的tcp超时都是用系统的时间设定,而且这个时间很长,超时的结果就是断开连接.和你应用要达到的目的显然差很 ...
- Bootstrap 新手学习笔记——布局组件
1.字形图标: <button type="button" class="btn btn-primary btn-lg" style="font ...