题目描述

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. 解决“cv2.error: OpenCV(3.4.2) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:356:...”

    主要是图片路径中“文件夹分隔符”使用的错误 将“\”改成“/”就好了 修改后的测试代码如下:x.py #导入cv模块 import cv2 as cv #读取图像,支持 bmp.jpg.png.tif ...

  2. ORM SQLAlchemy 简介

    对象关系映射(Object Relational Mapping,简称ORM使用DB-API访问数据库,需要懂 SQL 语言,能够写 SQL 语句,如果不想懂 SQL,又想使用关系型数据库,可以使用 ...

  3. 使 nodejs 代码 在后端运行(nohup)

    1.代码 nohup node server.js & 说明: nohup 命令对 server.js 进程做了三件事 (1)阻止SIGHUP信号发到这个进程. (2)关闭标准输入.该进程不再 ...

  4. Mac下持续集成-与JMeter与Ant执行后自动发送邮件的整合+定时任务

    mac定时任务的开启: Last login: Tue Aug 13 22:49:54 on ttys004 (base) localhost:~ ligaijiang$ sudo launchctl ...

  5. Sql题目精选练习

    1.每日经典sql 1.1.1 根据三张关系表查询雇员中工资最高的雇员的员工姓名.工资和部门号. salary(工资表) employee(员工表) department(部门表) Sql语句: SE ...

  6. realsense data

    line: (43,350) (558,350) (-0.448628,-0.554531,0.995),(0,0,0) 0.713282 ------------------------------ ...

  7. 【python画图】

    1.热力图 import numpy as np import numpy.random import matplotlib.pyplot as plt # Generate some test da ...

  8. 安装“Microsoft SQL Server 2014 Management Objects”时报错"Error Writing to file: Microsoft.SqlServer.XEvent.Linq.dll."

    问题: 当安装的软件依赖Microsoft SQL Server 2014 Management Objects时,会把这个组件打进安装包里,但是在服务器上安装时却报如下错误: “Error Writ ...

  9. iOS-意见反馈UITextView的使用+不能输入字符输入

    @interface DMFeedbackViewController ()<UITextViewDelegate,UIAlertViewDelegate>@property (nonat ...

  10. MySQL数据库相关资料

    python 全栈开发,Day60(MySQL的前戏,数据库概述,MySQL安装和基本管理,初识MySQL语句) python 全栈开发,Day61(库的操作,表的操作,数据类型,数据类型(2),完整 ...