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

思路:

主要是用map记录字符串出现次数,以及记录字符串对应的下标。

感觉写的太啰嗦,待优化。

vector<string> findRestaurant(vector<string>& list1, vector<string>& list2)
{
vector<string>res;
map<string,int>restaurant;//记录是否 饭店出现次数。如果>1
map<string,int>index1;//记录饭店1索引
map<string,int>index2;//记录饭店2索引
map<string,int>::iterator it;
for(int i=;i<list1.size();i++)
{
index1[list1[i]] = i;
restaurant[list1[i]]++;
}
for(int i=;i<list2.size();i++)
{
index2[list2[i]] = i;
restaurant[list2[i]]++;
}
int indexsum = ;
for(it =restaurant.begin();it!=restaurant.end();it++)
{
if(it->second ==)
{
if(index1[it->first] + index2[it->first] <indexsum)
{
indexsum =index1 [it->first] + index2[it->first];
res.clear();
res.push_back(it->first);
}
else if(index1[it->first] + index2[it->first] == indexsum)
{
indexsum =index1 [it->first] + index2[it->first];
res.push_back(it->first);
}
}
}
return res;
}

[leetcode-599-Minimum Index Sum of Two Lists]的更多相关文章

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

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

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

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

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

  4. [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 ...

  5. 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(easy)

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

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

  8. 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 fav ...

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

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

  10. [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. 【JAVAWEB学习笔记】24_filter实现自动登录和解决全局的编码问题

    过滤器Filter 学习目标 案例-自动登录 案例-解决全局的编码 一.过滤器Filter 1.filter的简介 filter是对客户端访问资源的过滤,符合条件放行,不符合条件不放行,并且可以对目标 ...

  2. [Open Source] RabbitMQ 高可用集群方案

    简介 RabbitMQ是用erlang开发的,集群非常方便,因为erlang天生就是一门分布式语言,但其本身并不支持负载均衡. Rabbit模式大概分为以下三种:单一模式.普通模式.镜像模式 Rabb ...

  3. Deep Learning in R

    Introduction Deep learning is a recent trend in machine learning that models highly non-linear repre ...

  4. Gradle 使用Maven本地缓存

    如果想使用Maven本地缓存,需要定义:build.gradle 文件下定义 build.gradle repositories { mavenLocal() } Gradle使用与Maven相同的策 ...

  5. 关于图的顶点染色问题的各种算法的C++实现之初探(一)——引言与简介

    我是一个数学工作者,专业方向是图论.研究图论已经十年有余.一个月前,一个偶然的机会让我萌生了一个念头,那就是我想尝试用C++写出我所学过的图论方面的算法.作为一个数学工作者,过去一直是纸上谈兵,我之前 ...

  6. sqlserver删除重复的数据

    分享链接: http://blog.csdn.net/s630730701/article/details/52033018 http://blog.csdn.net/anya/article/det ...

  7. flash2print文档在线预览应用(java,.net)

    一.背景 前段时间,LZ的boss突然给了出了这样一个需求:将原项目中的所有文章关联的附件TXT.PDF.office相关文件全部以flash的形式在网页上进行展示,便于预览.看似简单的需求,整个研发 ...

  8. 使用windows 命令行执行Git clone时出现Host key error

    由于是在java中执行cmd命令调用git clone,导致git读取不到用户的ssh key,需要设置环境变量Home为正确的用户路径: cmd /c set HOME=C:/Users/你的用户名 ...

  9. 刨根究底字符编码之十二——UTF-8究竟是怎么编码的

    UTF-8究竟是怎么编码的 1. UTF-8编码是Unicode字符集的一种编码方式(CEF),其特点是使用变长字节数(即变长码元序列.变宽码元序列)来编码.一般是1到4个字节,当然,也可以更长. 为 ...

  10. CRL快速开发框架4.4版发布,支持主从读写分离

    经过一些调整和优化,4.3已经运行在生产环境,对于不久将会遇到的查询性能,读写分离需求列上日程 读写分离需求 对于一个数据库作了主从发布/订阅,主库为DB1,从库为DB2 所有写入通过DB1,所有查询 ...