当前位置:网站首页>力扣解法汇总面试题 17.11-单词距离
力扣解法汇总面试题 17.11-单词距离
2022-06-12 02:03:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
有个内含单词的超大文本文件,给定任意两个不同的单词,找出在这个文件中这两个单词的最短距离(相隔单词数)。如果寻找过程在这个文件中会重复多次,而每次寻找的单词不同,你能对此优化吗?
示例:
输入:words = ["I","am","a","student","from","a","university","in","a","city"], word1 = "a", word2 = "student"
输出:1
提示:
words.length <= 100000
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/find-closest-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 遍历words,添加到map当中。key为单词,value为继承,存储单词对应的位置。 * 查找word1和word2之间最短距离时,找出两个对应的集合,求出两个集合之间的最小差即可。
代码:
public class SolutionMST1711 {
public int findClosest(String[] words, String word1, String word2) {
Map<String, List<Integer>> map = new HashMap<>();
for (int i = 0; i < words.length; i++) {
String word = words[i];
List<Integer> integers = map.get(word);
if (integers == null) {
integers = new ArrayList<>();
map.put(word, integers);
}
integers.add(i);
}
List<Integer> integers1 = map.get(word1);
List<Integer> integers2 = map.get(word2);
Collections.sort(integers1);
Collections.sort(integers2);
int minDiff = Integer.MAX_VALUE;
int index1 = 0;
int index2 = 0;
while (index1 < integers1.size() && index2 < integers2.size()) {
int integer1 = integers1.get(index1);
int integer2 = integers2.get(index2);
minDiff = Math.min(minDiff, Math.abs(integer1 - integer2));
if (integer1 >= integer2) {
index2++;
continue;
}
index1++;
}
return minDiff;
}
}边栏推荐
- Manually tear the linked list (insert, delete, sort) and pointer operation
- SQL calculates KS, AUC, IV, psi and other risk control model indicators
- How to stop anti-virus software from blocking a web page? Take gdata as an example
- [learn FPGA programming from scratch -20]: quick start chapter - operation steps 4-2-quick use of Altera quartz II tool (Modelsim co simulation, program download to altera development board)
- Wechat applet - a case of comparing the size of numbers
- State owned assets into shares, has Jianye real estate stabilized?
- LeetCode LCP 07. 传递信息
- Google Ads 的优势
- “中国东信杯”广西大学第四届程序设计竞赛(同步赛)
- LeetCode Algorithm 997. Find the town judge
猜你喜欢

超图倾斜数据合并根节点后转3dtiles

Dataset how to use dataset gracefully. After reading this article, you can fully understand the dataset in c7n/choerodon/ toothfish UI

BaseDexClassLoader那些事

Unit tests in golang

Graphical data analysis | business analysis and data mining

华为联运游戏或应用审核驳回:应用检测到支付serviceCatalog:X6

竞价广告每次点击出价多少钱是固定的吗?

Installing MySQL version 5.5 database for Linux (centos6)

Bracket generation (backtracking)

Ozzanmation action system based on SSE
随机推荐
商城开发知识点
Wide match modifier symbol has been deprecated, do not use
[learn FPGA programming from scratch -20]: quick start chapter - operation steps 4-2-quick use of Altera quartz II tool (Modelsim co simulation, program download to altera development board)
Niuke monthly race 14- simple counting
超图倾斜数据合并根节点后转3dtiles
初探性能优化!从2个月到4小时的性能提升!
阿里云oss文件上传系统
Navicat for MySQL 11 Linux 破解方法
Modification of system module information of PHP security development 12 blog system
Subject knowledge and educational ability of information technology in the first half of 2019 – subjective questions
Introduction to SVM
matplotlib. pyplot. Bar chart (II)
Fatal error in launcher: unable to create process using
php安全开放10文章列表模块的分页编写
Tiobe - programming language ranking in June 2022
Linux(CentOS7)安裝MySQL-5.7版本
PHP development 09 article module deletion and article classification writing
MySQL advanced knowledge points
android html5页面加载缓存优化
聯調這夜,我把同事打了...