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 ...
随机推荐
- ICEM—气体罐子
原视频下载地址:https://yunpan.cn/cPfzaLzkcFXVK 访问密码 62cf
- jenkins的任务卡住
今天做jenkins任务的时候,发现一个启动后,一直卡住,在那转圈圈,其实这个时候,任务已经执行完了. 经过分析,因为这个任务是启动一个web服务,直接在机器上执行时,直接占用一个终端. 解决办法,放 ...
- 我对java多进程多线程的浅显理解
linux内核调度算法--CPU时间片如何分配: http://blog.csdn.net/russell_tao/article/details/7103012(转载) ,通过该文章我们知道了进程 ...
- golang gorm框架的默认时区问题
gorm框架的时区是在连接数据库时设置的, 如下
- git 新建仓库第一次提交
1 . git init //初始化仓库,在初始化的目录中会出现.git的文件夹 2. git add .(文件name) //添加文件到本地仓 3. git commit -m "fi ...
- 【SpringBoot】整体下载大文件与分批下载大文件(利用MySql数据库的Limit实现)
在前文里谈过一次性从数据库取一个大结果集有可能导致outofMemory,当时的想法是分批去取回来,今天把它实现了,特地把代码分享出来: 工程下载:https://files.cnblogs.com/ ...
- 《maven实战》笔记(4)----maven的仓库
maven的构件表示方式是文件,maven通过仓库来统一管理这些文件. maven仓库的布局方式: 任何一个构件都有其唯一的坐标,根据这个坐标可以定义其在仓库中的唯一存储路径 仓库分为两类:本地仓库和 ...
- 使用idea进行activiti工作流开发入门学习
1.安装插件 在idea里面,activiti的插件叫actiBPM,在插件库里面把它安装好,重启idea就行了. 2.新建一个maven项目,并更改pom.xml.pom中依赖如下: <?xm ...
- 阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_10-修改页面-前端-修改页面
1.进入页面,通过钩子方法请求服务端获取页面信息,并赋值给数据模型对象 2.页面信息通过数据绑定在表单显示 3.用户修改信息点击“提交”请求服务端修改页面信息接口 3.3.3 修改页面 3.3.3.1 ...
- 配置WEB错误页面
项目运行时,难免会出现错误,这些错误我们不可以也不方便直接让用户看到,所以配置错误页面是非常必要的. 一下是项目的Web.xml文件,在最下方阴影部分是配置错误界面. <?xml version ...