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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [LeetCode] Shortest Word Distance 最短单词距离

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  4. LeetCode Shortest Word Distance III

    原题链接在这里:https://leetcode.com/problems/shortest-word-distance-iii/ 题目: This is a follow up of Shortes ...

  5. LeetCode Shortest Word Distance II

    原题链接在这里:https://leetcode.com/problems/shortest-word-distance-ii/ 题目: This is a follow up of Shortest ...

  6. LeetCode Shortest Word Distance

    原题链接在这里:https://leetcode.com/problems/shortest-word-distance/ 题目: Given a list of words and two word ...

  7. 245. Shortest Word Distance III

    题目: This is a follow up of Shortest Word Distance. The only difference is now word1 could be the sam ...

  8. 244. Shortest Word Distance II

    题目: This is a follow up of Shortest Word Distance. The only difference is now you are given the list ...

  9. [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 ...

  10. [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 ...

随机推荐

  1. Future模式

    Future模式简介 Future模式有点类似于网上购物,在你购买商品,订单生效之后,你可以去做自己的事情,等待商家通过快递给你送货上门.Future模式就是,当某一程序提交请求,期望得到一个答复.但 ...

  2. Java-日期转换

    如下: package 时间日期类; import java.text.SimpleDateFormat; import java.util.Date; public class 日期格式转换 { / ...

  3. UVALive 6523 Languages

    传送门 The Enterprise has encountered a planet that at one point had been inhabited. The only remnant f ...

  4. jQuery EasyUI API 中文文档

    http://www.cnblogs.com/Philoo/tag/jQuery/ 共2页: 1 2 下一页  jQuery EasyUI API 中文文档 - 树表格(TreeGrid) 风流涕淌 ...

  5. MySQL force Index 强制索引概述

    以下的文章主要介绍的是MySQL force Index  强制索引,以及其他的强制操作,其优先操作的具体操作步骤如下:我们以MySQL中常用的hint来进行详细的解析,如果你是经常使用Oracle的 ...

  6. html5新标签转

    HTML 5 是一个新的网络标准,目标在于取代现有的 HTML 4.01, XHTML 1.0 and DOM Level 2 HTML 标准.它希望能够减少浏览器对于需要插件的丰富性网络应用服务(p ...

  7. 使用key链接远程Git仓库

    使用密钥来访问Git仓库比密码安全多了,只要把公钥配置在远程端,把密钥放到~/.ssh/里以id_rsa命名即可. 不过有人要问如果有多个仓库,而且用了不同密钥怎么办? 这时候可以在~/.ssh/文件 ...

  8. linux下python版webshell后门查杀工具

    使用说明: 1.查杀指定路径:python webshell.py 路径 2.按时间查找文件:python webshell.py 路径 “2013-09-28 00:00:00″ # -*- cod ...

  9. nginx负载均衡的配置方法

    upstream www.***.com { server ; server ; server ; } server { listen ; server_name www.***.com; #char ...

  10. linux 运行级别与chkconfig

    一.Linux的运行级别 在装MySQL的时候,才知道了Linux的运行级别这么一回事.汗…自己太水了…下面总结一下: 什么是运行级别呢?简单点来说,运行级别就是操作系统当前正在运行的功能级别.级别是 ...