Leetcode简单题41~60
41.思路:分别在列表中存放两个单词的索引,再用两次遍历求距离# 给定一个单词列表和两个单词 word1 和 word2,返回列表中这两个单词之间的最短距离。# 示例:# 假设 words = ["practice", "makes", "perfect", "coding", "makes"]# 输入: word1 = “coding”, word2 = “practice”# 输出: 3# 输入: word1 = "makes", word2 = "coding"# 输出: 1#meclass Solution0(object): def shortestDistance(self, words, word1, word2): result = [] idwords1 = [id_ for id_,word in enumerate(words) if word == word1] idwords2 = [id_ for id_,word in enumerate(words) if word == word2] for id1 in idwords1: for id2 in idwords2: result.append(abs(id1-id2)) return min(result)#other 复杂度O(n)class Solution1(object): def