599、Minimum Index Sum of Two Lists 两个列表的最小索引总和
难度:简单
题目描述
英文:
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.
中文:
假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示。
你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅。 如果答案不止一个,则输出所有答案并且不考虑顺序。 你可以假设总是存在一个答案。
示例
Example 1:
1 | Input: |
Example 2:
1 | Input: |
提示
- The length of both lists will be in the range of [1, 1000].
- The length of strings in both lists will be in the range of [1, 30].
- The index is starting from 0 to the list length minus 1.
- No duplicates in both lists.
代码提交
Python2,用时128ms,内存11.1M
1 | class Solution(object): |
Tips
Python查找dict的key时,应使用if key in dict
,而不是if key in dict.keys()
。
前者是在dict中查找,dict对象的存储结构是hash表,最优情况下查询复杂度为O(1);
后者等于是在list中查找,list对象的存储结构是线性表,查询复杂度为O(n)。