LC 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.
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的更多相关文章
- 【Leetcode_easy】599. Minimum Index Sum of Two Lists
problem 599. Minimum Index Sum of Two Lists 题意:给出两个字符串数组,找到坐标位置之和最小的相同的字符串. 计算两个的坐标之和,如果与最小坐标和sum相同, ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
- 【LeetCode】599. Minimum Index Sum of Two Lists 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:找到公共元素再求索引和 方法二:索引求和,使 ...
- LeetCode 599: 两个列表的最小索引总和 Minimum Index Sum of Two Lists
题目: 假设 Andy 和 Doris 想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. Suppose Andy and Doris want to cho ...
- [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 ...
随机推荐
- AGC024E Sequence Growing Hard
题意 给出\(n\),\(m\),\(mu\),问有多少个序列组\((A_0,A_1,\dots,A_n)\)满足: 序列\(Ai\)的长度恰好为\(i\) 所有元素均在\([1,m]\) \(A_{ ...
- Flask 四种响应类型
1 直接返回字符串 可以返回状态码 @app.route('/testresponse', methods=['GET', 'POST']) def testresponse(): return &q ...
- maven的pom报错web.xml is missing and <failOnMissingWebXml> is set to true
错误信息:web.xml is missing and <failOnMissingWebXml> is set to true 解决办法:https://blog.csdn.net/si ...
- sqlserver 锁表进程及执行的SQL
--1.查进程select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableNamefrom sys.d ...
- C++的精髓——代码复用、接口复用
C++的精髓——代码复用.接口复用 在另一篇文章中提到C++三大特点的核心概括,也写在这里吧.封装:信息隐藏继承:代码复用多态:面向对象C++并不是面向对象,它包容多种编程思想,如面向过程,面向对象, ...
- dll程序开发总结
1.修改生成的dll名称 VS2012中选中某个项目,项目--属性--配置属性--连接器--常规--输出文件
- 例子:Vue 配合 vue-resource 从接口获取数据
vue-resource 是 vue 的一个与服务器端通信的 HTTP 插件,用来从服务器端请求数据. 结合例子——图片列表来写一下 Vue获取接口数据. html : <div id=&quo ...
- drawable,andrid 圆形图片,圆角图片
各种drawable 小的例子, 开源中国地址:https://git.oschina.net/lyxs916/drawable-test.git bitm ...
- 关于ios在H5页面长按识别二维码无效
环境 框架: vue 操作系统: ios 解决 如果你的vue-router用了mode:history: 把它去掉用默认的就OK了
- java网络通信:异步非阻塞I/O (NIO)
转: java网络通信:异步非阻塞I/O (NIO) 首先是channel,是一个双向的全双工的通道,可同时读写,而输入输出流都是单工的,要么读要么写.Channel分为两大类,分别是用于网络数据的S ...