LeetCode 244. Shortest Word Distance II(最短单词距离)
2016-04-07 05:28
627 查看
原题网址: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 =
Given word1 =
return 3.
Given word1 =
return 1.
Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
思路:先记录每个单词在数组中的位置,然后对两个单词的所有位置顺序扫描。
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");
相关文章推荐
- lintcode-easy-Ugly Number
- #8判断和循环#
- Xsolla 电子竞技学院合作项目正式启动:你加入了么?
- vs2015中升级Nuget后,找不到Umbraco安装包问题
- Windows的自带控件(比如TButton)大多数消息都由它自己处理,Delphi覆盖了那么多WM_函数优先级较低,一般用不上
- liunx 下scp出现 Permission denied, please try again 的解决办法
- Windows下Keras报错TypeError: <lambda>() got an unexpected keyword argument 'name'
- HttpClient
- Android属性动画
- swift 二维数组
- Nexus启动失败处理:The nexus service was launched, but failed to start.
- VB洗牌算法产生随机数组
- 五种情况下会刷新控件状态(刷新所有子FWinControls的显示)——从DFM读取数据时、新增加子控件时、重新创建当前控件的句柄时、设置父控件时、显示状态被改变时
- LeetCode 243. Shortest Word Distance(单词最短距离)
- Java发送邮件
- lintcode-medium-Subsets II
- 【AJAX】使用ajaxStart()和ajaxStop()方法
- ANDROID——仿360手机卫士的旋转打分控件
- ANDROID——仿360手机卫士的旋转打分控件
- ANDROID——仿360手机卫士的旋转打分控件