# LeetCode 599. 两个列表的最小索引总和

## 题目地址(599. 两个列表的最小索引总和)

leetcode-cn.com/problems/mi…

## 题目描述

假设Andy和Doris想在晚餐时选择一家餐厅，并且他们都有一个表示最喜爱餐厅的列表，每个餐厅的名字用字符串表示。

["Shogun", "Tapioca Express", "Burger King", "KFC"]
["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"]

["Shogun", "Tapioca Express", "Burger King", "KFC"]
["KFC", "Shogun", "Burger King"]

## 代码

• 语言支持：Python3

Python3 Code:


class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
resDict = dict()
resList = list()
for index,val in enumerate(list1):
resDict[val] = index
for index,val in enumerate(list2):
if val in resDict:
resList.append((index+resDict[val],val))
#对index总和排序，得到最低权重的值，然后把相同值抽取出来
resList.sort(key=lambda x:x[0])
res = []
resIndex = resList[0][0]
for indexCount,val in resList:
if indexCount == resIndex:
res.append(val)
return res

if __name__ == '__main__':
list1 = ["Shogun", "Tapioca Express", "Burger King", "KFC"]
list2 = ["KFC", "Shogun", "Burger King"]
ret = Solution().findRestaurant(list1,list2)
print(ret)

• 时间复杂度：$O(n)$
• 空间复杂度：$O(n)$