美文网首页
leetcode 599. 两个列表的最小索引总和

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

作者: fanchuang | 来源:发表于2020-03-13 01:29 被阅读0次
class Solution:
    def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
        # 如果使用字典,先都统计出来,然后再排序,也是可行的,但是我的这种写法也是很有趣的。
       
        sum_of_index = 0 
        ret = []
        for i in list1:
            if i in list2:
                list1.index(i) + list2.index(i)
                # d[i] =  list1.index(i) + list2.index(i) 
                # ret.append(i)

                if list1.index(i) + list2.index(i)  == sum_of_index:
                    ret.append(i) 

                if list1.index(i) + list2.index(i) > sum_of_index:
                    if not ret:
                        ret.append(i)
                        sum_of_index = list1.index(i) + list2.index(i)

                if list1.index(i) + list2.index(i) < sum_of_index:
                    if ret:
                        ret.clear()
                        ret.append(i)

                         
        # print(ret)
        return ret 

相关文章

网友评论

      本文标题:leetcode 599. 两个列表的最小索引总和

      本文链接:https://www.haomeiwen.com/subject/ccsojhtx.html