599. 两个列表的最小索引总和
为保证权益,题目请参考 599. 两个列表的最小索引总和(From LeetCode).
解决方案1
Python
python
# 599. 两个列表的最小索引总和
# https://leetcode-cn.com/problems/minimum-index-sum-of-two-lists/
from typing import List
class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
d1 = dict([[v,i] for i,v in enumerate(list1)])
ans = []
minSum = float('inf')
for i,v in enumerate(list2):
if v in d1:
if d1[v] + i < minSum:
minSum = d1[v] + i
ans = [v]
elif d1[v] + i == minSum:
ans.append(v)
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21