LeetCode 244. Shortest Word Distance II(最短单词距离)
来源:互联网 发布:js 数组变量 编辑:程序博客网 时间:2024/05/23 00:15
原题网址:https://leetcode.com/problems/shortest-word-distance-ii/
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it?
Design a class which receives a list of words in the constructor, and implements a method that takes two words word1 and word2 and 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.
public class WordDistance { private Map<String, List<Integer>> map = new HashMap<>(); public WordDistance(String[] words) { for(int i=0; i<words.length; i++) { List<Integer> positions = map.get(words[i]); if (positions == null) { positions = new ArrayList<>(); map.put(words[i], positions); } positions.add(i); } } public int shortest(String word1, String word2) { int distance = Integer.MAX_VALUE; List<Integer> positions1 = map.get(word1); List<Integer> positions2 = map.get(word2); int i=0, j=0; while (i<positions1.size() && j<positions2.size()) { if (positions1.get(i) < positions2.get(j)) { if (positions2.get(j)-positions1.get(i) < distance) distance = positions2.get(j)-positions1.get(i); i ++; } else { if (positions1.get(i)-positions2.get(j) < distance) distance = positions1.get(i)-positions2.get(j); j ++; } } return distance; }}// Your WordDistance object will be instantiated and called as such:// WordDistance wordDistance = new WordDistance(words);// wordDistance.shortest("word1", "word2");// wordDistance.shortest("anotherWord1", "anotherWord2");
- LeetCode 244. Shortest Word Distance II(最短单词距离)
- LeetCode 245. Shortest Word Distance III(最短单词距离)
- LeetCode 243. Shortest Word Distance(单词最短距离)
- LeetCode-243:Shortest Word Distance (单词最短距离)
- LeetCode 244. Shortest Word Distance II
- LeetCode 244. Shortest Word Distance II
- leetcode--Shortest Word Distance II
- Leetcode: Shortest Word Distance II
- LeetCode-Shortest Word Distance II
- LeetCode Shortest Word Distance II
- leetcode--Shortest Word Distance II
- LeetCode 317. Shortest Distance from All Buildings(最短建筑距离)
- 244. Shortest Word Distance II
- 244. Shortest Word Distance II
- 244. Shortest Word Distance II
- [leetcode] 244. Shortest Word Distance II 解题报告
- [Leetcode] 244. Shortest Word Distance II 解题报告
- LeetCode 题解(268) : Shortest Word Distance II
- swift 二维数组
- Android属性动画
- Xsolla 电子竞技学院合作项目正式启动:你加入了么?
- #8判断和循环#
- ios中的定时
- LeetCode 244. Shortest Word Distance II(最短单词距离)
- 关于Windhow
- LeetCode 245. Shortest Word Distance III(最短单词距离)
- LeetCode 246. Strobogrammatic Number
- LeetCode 247. Strobogrammatic Number II
- 关系型数据库工作原理-事务管理(二)(翻译自Coding-Geek文章)
- java和js互调
- Bzoj2084:[Poi2010]Antisymmetry:manachar算法
- 检查OGG占用端口的命令