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 ...
随机推荐
- [转]Oracle数据库中的约束
SQL 约束 约束用于限制加入表的数据的类型. 可以在创建表时规定约束(通过 CREATE TABLE 语句),或者在表创建之后也可以(通过 ALTER TABLE 语句). 我们将主要探讨以下几种约 ...
- poj2752 KMP
需要理解next[]的意义.之前看到大牛的博客,next[]讲的非常清楚. 利用next[],当前位子的前面那一段和next[当前位子]的前面那一段是相同的.又next[next[当前位子]]与nex ...
- Tomcat 使用说明
Tomcat下有7个目录,分别是bin,conf,lib,logs,temp,webapps,work 目录 Tomcat根目录在tomcat中叫<CATALINA_HOME> 1.< ...
- Oracle数据库语句大全
转自:http://blog.sina.com.cn/s/blog_b5d14e2a0101c56z.html ORACLE支持五种类型的完整性约束 NOT NULL (非空)--防止NULL值进入指 ...
- Teradata(不同date输出要求;表类型)
1. 需要某种特定形式的date 类型export 到文件中,例如 YYYYMMDD/ YYYY-MM-DD 这时候不一定非要用date 类型,可以转换为varchar 类型! CAST(CAST ( ...
- 理解SIFT
理解SIFT.tab{font-size:12px; margin-bottom: 10px;}.tab a{cursor:pointer;cursor:pointer;display:inline- ...
- foeach集合遍历
package number; public class Number { public static void main(String[] args) { int[] arr={5,2,1,0,3, ...
- H2嵌入式数据库
一 H2 数据库 官网地址. http://www.h2database.com/html/cheatSheet.html
- POJ2965The Pilots Brothers' refrigerator(枚举+DFS)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22057 ...
- 如何获取xcassets中LaunchImage图片
NSDictionary * dic = @{@"320x480" : @"LaunchImage-700", @"320x568" : @ ...