题目描述
有个内含单词的超大文本文件,给定任意两个不同的单词,找出在这个文件中这两个单词的最短距离(相隔单词数)。如果寻找过程在这个文件中会重复多次,而每次寻找的单词不同,你能对此优化吗?
示例:
输入: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
方法一:
一次遍历
class Solution {
public:
int findClosest(vector<string>& words, string word1, string word2) {
int a=-1,b=-1,num=100001;
for(int i=0;i<words.size();i++){
if(words[i]==word1){
a=i;
if(b!=-1) num=abs(a-b)>num?num:abs(a-b);
}
if(words[i]==word2){
b=i;
if(a!=-1) num=abs(a-b)>num?num:abs(a-b);
}
}
return num;
}
};