当前位置:网站首页>Force deduction solution summary interview question 17.11- word distance
Force deduction solution summary interview question 17.11- word distance
2022-06-12 02:09:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
https://github.com/September26/java-algorithms
Original link : Power button
describe :
There's a huge text file with words , Given any two different words , Find the shortest distance between the two words in this file ( Number of words separated ). If the search process is repeated many times in this file , And each time I look for a different word , Can you optimize this ?
Example :
Input :words = ["I","am","a","student","from","a","university","in","a","city"], word1 = "a", word2 = "student"
Output :1
Tips :
words.length <= 100000
source : Power button (LeetCode)
link :https://leetcode.cn/problems/find-closest-lcci
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * Traverse words, Add to map among .key For the word ,value For inheritance , Store the position corresponding to the word . * lookup word1 and word2 At the shortest distance between , Find two corresponding sets , Find the minimum difference between two sets .
Code :
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;
}
}边栏推荐
- Force deduction solution summary 358 Mini parser
- Implementation scheme of iteration and combination pattern for general tree structure
- Graphic data analysis | business cognition and data exploration
- 力扣解法汇总965-单值二叉树
- "China Dongxin Cup" the fourth programming competition of Guangxi University (synchronous competition)
- Glfwpollevents() program crash
- 微信公众号开发地理位置坐标的转换
- “中国东信杯”广西大学第四届程序设计竞赛(同步赛)
- Graphical data analysis | business analysis and data mining
- 力扣解法汇总358-迷你语法分析器
猜你喜欢

leetcodeSQL:612. Nearest distance on plane

Graphical data analysis | business analysis and data mining

混泥土(地面+墙面)+ 山体裂缝数据集汇总(分类及目标检测)

Alicloud OSS file upload system

自适应搜索广告有哪些优势?

Does the virtual host have independent IP

How WPS inserts a directory and the operating steps for quickly inserting a directory

Is the bidding price fixed for each click?

The establishment and introduction of the announcement module of PHP development blog system

Knowledge points of mall development
随机推荐
超图倾斜数据合并根节点后转3dtiles
How to automatically color cells in Excel
Three main factors determining advertising quality
【无标题】2022煤矿安全检查考题及在线模拟考试
Metaverse × How will smart cities develop?
力扣解法汇总965-单值二叉树
Graphic data analysis | business cognition and data exploration
2022最全面的Redis事务控制(带图讲解)
高考完不要急着去打工了,打工以后有的是机会,不差这三个月
力扣解法汇总450-删除二叉搜索树中的节点
“中国东信杯”广西大学第四届程序设计竞赛(同步赛)
SwiftyJSON解析本地JSON文件
The force deduction solution summarizes the shortest distance of 821 characters
Graphic data analysis | data cleaning and pretreatment
力扣解法汇总675-为高尔夫比赛砍树
Glfwpollevents() program crash
2022西式面点师(技师)复训题库及在线模拟考试
Force deduction solution summary 467- unique substring in surrounding string
Don't miss it! Five large data visualization screens that HR must collect
C language programming classic games - minesweeping