Given a list of words and two words word1 and word2, 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.
题意:给定一个数组和两个单词word1,word2,返回两者最近的距离
word1不等于word2,但是两者可能在数组中出现多次
分类:数组
解法1:遍历数组,当遍历到word1,word2,记录下位置,并且每次都计算最新的最小值
原文链接http://blog.csdn.net/crazy__chen/article/details/47859737
最短单词距离算法
本文介绍了一种算法,用于计算给定数组中两个指定单词之间的最短距离。通过一次遍历数组并记录两单词的位置来高效解决问题。适用于数组中单词多次出现的情况。
449

被折叠的 条评论
为什么被折叠?



