题目
有个内含单词的超大文本文件,给定任意两个单词,找出在这个文件中这两个单词的最短距离(相隔单词数)。如果寻找过程在这个文件中会重复多次,而每次寻找的单词不同,你能对此优化吗?
示例:
输入:words = ["I","am","a","student","from","a","university","in","a","city"], word1 = "a", word2 = "student"
输出:1
分析: 一种解法是采用双指针,遍历数组,记录找到的单词的位置,两个指针单词不同则计算中间的距离,相同则更新前指针位置到后指针,后指针继续遍历。第二种方法是分别记录下两个单词在文件中出现的所有坐标,然后计算两个坐标组之间的最小距离。
方法1: 双指针
class Solution:
def findClosest(self, words: List[str], word1: str, word2: str) -> int:
word1_loc = []
word2_loc = []
for i in range(len(words)):
if words[i] == word1:
word1_loc.append(i)
elif words[i] == word2:
word2_loc.append(i)
i, j = 0, 0
min_distance = len(words)
while i < len(word1_loc) and j < len(word2_loc):
min_distance = min(min_distance, abs(word1_loc[i]-word2_loc[j]))
if word1_loc[i] > word2_loc[j]:
j += 1
else:
i += 1
return min_distance
方法2:
class Solution:
def findClosest(self, words: List[str], word1: str, word2: str) -> int:
word1_loc = []
word2_loc = []
for i in range(len(words)):
if words[i] == word1:
word1_loc.append(i)
elif words[i] == word2:
word2_loc.append(i)
i, j = 0, 0
min_distance = len(words)
while i < len(word1_loc) and j < len(word2_loc):
min_distance = min(min_distance, abs(word1_loc[i]-word2_loc[j]))
if word1_loc[i] > word2_loc[j]:
j += 1
else:
i += 1
return min_distance