Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings.

You need to help them find out their common interest with the least list index sum. If there is a choice tie between answers, output all of them with no order requirement. You could assume there always exists an answer.

Example 1:

Input:
["Shogun", "Tapioca Express", "Burger King", "KFC"]
["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"]
Output: ["Shogun"]
Explanation: The only restaurant they both like is "Shogun".

Example 2:

Input:
["Shogun", "Tapioca Express", "Burger King", "KFC"]
["KFC", "Shogun", "Burger King"]
Output: ["Shogun"]
Explanation: The restaurant they both like and have the least index sum is "Shogun" with index sum 1 (0+1).

Note:

  1. The length of both lists will be in the range of [1, 1000].
  2. The length of strings in both lists will be in the range of [1, 30].
  3. The index is starting from 0 to the list length minus 1.
  4. No duplicates in both lists.
 
class Solution(object):
def findRestaurant(self, list1, list2):
"""
:type list1: List[str]
:type list2: List[str]
:rtype: List[str]
"""
ans=[]
ma=100000
n=len(list1)
for i in range(n):
s=list1[i]
if s in list2:
j=list2.index(s)
if i+j<=ma:
ma=i+j
ans.append(s)
return ans

  

最新文章

  1. 云服务器 Centos7.0 部署
  2. FastJson的使用
  3. aspxshell下突破无可写可执行目录执行cmd
  4. Sublime Text3 常用快捷键
  5. Bootstrap3.0学习第十一轮(输入框组)
  6. 通过硬件层提高Android动画的性能
  7. double类型如何保留2为小数
  8. IntelliJ IDEA javaDoc的使用
  9. Go 语言编写单元测试
  10. springboot~lombok使用总结
  11. 论学习IT的基本学习方法
  12. 洛谷P1048采药题解
  13. shell脚本--权限分配
  14. 合并两个sorted ranges(merge和inplace_merge)
  15. idea本地将本地现有的项目和gitlab进行管理并提交到线上
  16. C#提高--------------获取方法返回值的自定义特性(Attribute)
  17. 什么叫做hack
  18. Java字符串池
  19. [可行]setoolkit生成木马软件远程控制实例
  20. Qt事件系统基本概念

热门文章

  1. ID基本操作(新建文档,页面编码)5.8
  2. python 自然语言处理(二)____获得文本语料和词汇资源
  3. LTP(LinuxTest Project)测试工具
  4. 女生可不可以进入IT行业做Linux运维工程师?
  5. 读书笔记 C# Type类型与泛型有关的某些属性浅析
  6. Excel 数据读入到DataSet
  7. linux系统安装tomcat详细配置
  8. 第一个python程序--hello,world
  9. Spring学习三
  10. leetcode第四题:两个有序数组的中位数