leetcode--Shortest Word Distance II
来源:互联网 发布:java android pdf 编辑:程序博客网 时间:2024/05/14 20:09
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:
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.
题意:给定数组,和两个word,word,要求构造一个类,计算两者的最短距离,因为我们要给出很多组不同的两个word,你会怎么组织这个类呢?
分类:数组
解法1:在构造函数,使用hashMap来记录每个单词的位置
在计算最短距离的函数中,两个列表是已排序的,因此可使用类似MergeSort的思路求解最短距离。
public class WordDistance { private HashMap<String, List<Integer>> indexer = new HashMap<String, List<Integer>>(); public WordDistance(String[] words) { if (words == null) return; for (int i = 0; i < words.length; i++) { if (indexer.containsKey(words[i])) { indexer.get(words[i]).add(i); } else { List<Integer> positions = new ArrayList<Integer>(); positions.add(i); indexer.put(words[i], positions); } } } public int shortest(String word1, String word2) { List<Integer> posList1 = indexer.get(word1); List<Integer> posList2 = indexer.get(word2); int i = 0, j = 0; int diff = Integer.MAX_VALUE; while (i < posList1.size() && j < posList2.size()) { int pos1 = posList1.get(i), pos2 = posList2.get(j); if (pos1 < pos2) { diff = Math.min(diff, pos2 - pos1); i++; } else { diff = Math.min(diff, pos1 - pos2); j++; } } return diff; }}
0 0
- 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 题解(268) : Shortest Word Distance II
- LeetCode 244. Shortest Word Distance II
- LeetCode 244. Shortest Word Distance II
- Shortest Word Distance II
- Shortest Word Distance II
- Shortest Word Distance II
- leetcode--Shortest Word Distance
- Leetcode: Shortest Word Distance
- *LeetCode-Shortest Word Distance
- LeetCode Shortest Word Distance
- Leetcode Shortest Word Distance
- leetcode--Shortest Word Distance
- [LeetCode244]Shortest Word Distance II
- 书摘---网络搜集整理 (11--20)
- C#操作datatable
- opencv学习笔记 split(图像分割为3通道)
- Android Studio 'The same input jar is specified twice'
- 三次握手协议
- leetcode--Shortest Word Distance II
- 从cpan上安装perl模块
- 将一个数组中的值按逆序重新存放
- pat-旧键盘
- project.pbxproj 文件的学习
- 【C++/数据结构】顺序表的基本操作
- linux下tmpfs文件系统简介
- 安装JavaJDK,三步搞定。
- Maximum Value(Codeforces_484B)