题目描述

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).

参考答案

 class Solution {
public:
vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) { vector<string> res;
unordered_map<string, int> map;
for(size_t i = ; i< list1.size(); i++)
map[list1[i]] = i ; // list1 = key ; i = value ; size_t min = INT_MAX; for(size_t i = ; i<list2.size();i ++){
auto iter = map.find(list2[i]);
if(iter != map.end()){
if(iter->second + i< min ){
min = map[list2[i]] + i;
res.assign(,list2[i]); // 直接把值赋给第一个,不需要clear然后重新赋值 }else if(iter->second + i == min){
res.push_back(list2[i]);
} }
}
return res;
}
};

分析备注

1.  for 循环中,int 替换为 size_t 可以加速。

2.  vector.assign(1,value) 可以实现 清除+赋值 两步操作。

3.  使用 iter -> first 和 iter -> second 可以更快。

LC 599. Minimum Index Sum of Two Lists的更多相关文章

  1. 【Leetcode_easy】599. Minimum Index Sum of Two Lists

    problem 599. Minimum Index Sum of Two Lists 题意:给出两个字符串数组,找到坐标位置之和最小的相同的字符串. 计算两个的坐标之和,如果与最小坐标和sum相同, ...

  2. LeetCode 599. Minimum Index Sum of Two Lists (从两个lists里找到相同的并且位置总和最靠前的)

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

  3. 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 ...

  4. 599. Minimum Index Sum of Two Lists(easy)

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

  5. [LeetCode&Python] Problem 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 ...

  6. 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 fa ...

  7. 【LeetCode】599. Minimum Index Sum of Two Lists 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:找到公共元素再求索引和 方法二:索引求和,使 ...

  8. LeetCode 599: 两个列表的最小索引总和 Minimum Index Sum of Two Lists

    题目: 假设 Andy 和 Doris 想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. Suppose Andy and Doris want to cho ...

  9. [LeetCode] 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 ...

随机推荐

  1. CONTINUE...? ZOJ - 4033

    CONTINUE...? ZOJ - 4033 题解:先计算一下总数sum是否是偶数,如果不是则不能分出来,如果是则从后面开始分,先把人分到1.3组,分完sum / 2这些人,如果恰好能够分成零,那么 ...

  2. SNMP 协议介绍 转载

    一.SNMP简单概述 1.1.什么是Snmp SNMP是英文"Simple Network Management Protocol"的缩写,中文意思是"简单网络管理协议& ...

  3. arcgis根据表字段进行数据合并

    第一步 1.地理处理-----2.数据管理工具----3.制图综合----4.融合 第二步 打开融合面板,选择输入要素,要融合的字段,选择统计字段数量,完成融合.

  4. DSSM算法-计算文本相似度

    转载请注明出处: http://blog.csdn.net/u013074302/article/details/76422551 导语 在NLP领域,语义相似度的计算一直是个难题:搜索场景下quer ...

  5. Linux 系统配置永久性时间同步

    临时修改系统时间(reboot后系统时间恢复): date 查看系统时间 date -s  "设置的系统时间" 永久性修改系统时间: date 查看系统时间 hwclock --s ...

  6. python将py文件转换为pyc

    python -m py_compile lib/ylpy.py python -m py_compile lib/ylpy.py python 一个.py文件如何调用另一个.py文件中的类和函数 A ...

  7. 如何查看linux内核中驱动的初始化顺序?

    答:通过生成的System.map可以查看到,主要关注__initcall_<module_entry_function>_init<level>,如: __initcall_ ...

  8. [go]template使用

    //index.html {{if gt .Age 18}} <p>hello, old man, {{.Name}}</p> {{else}} <p>hello, ...

  9. firewalld介绍

    (1).什么是firewalld? firewalld是提供了支持网络/防火墙区域(zone)定义网络链接以及接口安全等级的动态防火墙管理工具. (2).firewalld与iptables之间的关系 ...

  10. 前端如何避免bug的产生?

    项目环境:react生态圈 界面功能基本和:增(新增一条数据).删(删除一条数据).查(展示列表).改(修改数据)挂钩. 一.展示数据列表相关[判空,控制显示距离,分页是否有效,搜索是否有效] 1.渲 ...