599Minimum Index Sum of Two Lists

来源:互联网 发布:绝对伏特加 知乎 编辑:程序博客网 时间:2024/05/17 22:36
class Solution(object):
    def findRestaurant(self, A, B):
        """
        :type list1: List[str]
        :type list2: List[str]
        :rtype: List[str]
        """
        dic={u:i for i,u in enumerate(A)}
        best, ans = 10000, []
        for j in range(len(B)):
            if B[j] in dic:
                i = dic[B[j]]
                if i + j < best:
                    best = i + j
                    ans = [B[j]]
                elif i + j == best:
                    ans.append(B[j])

        return ans

找两列表中下表和最小的元素