当前位置:网站首页>Force buckle 599 Minimum index sum of two lists

Force buckle 599 Minimum index sum of two lists

2022-07-07 20:06:00 Tomorrowave

599. The minimum index sum of two lists

hypothesis Andy and Doris Want to choose a restaurant for dinner , And they all have a list of their favorite restaurants , The name of each restaurant is represented by a string .

You need to help them use the least index and find their favorite restaurants . If there is more than one answer , Then all the answers are output regardless of the order . You can assume that the answer always exists .

Knowledge points involved :

Dictionaries

Code section

class Solution:
    def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
        favorite_restaurant={
    }
        min_f=[]
        for x in list1:
            if x in list2:
                favorite_restaurant[x]=list2.index(x)+list1.index(x)
        minv=min(favorite_restaurant.values())
        for i,j in favorite_restaurant.items():
            if j==minv:
                min_f.append(i)

        return min_f
原网站

版权声明
本文为[Tomorrowave]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071753357045.html