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. 版本管理工具SVN学习(一):简单的SVN命令,兼对比Git

    新公司用SVN来管理代码,而且公司自己搭建了SVN服务器,所以要学习下SVN的相关命令.服务器搭建等技能知识.上家公司是用Git管理代码,而且代码托管在git@oschina上,自然不用操心Git服务 ...

  2. Android的cookie的接收和发送

    我在做自动登录的时候遇到的坑,特写此文以提醒各位不要把自己绕进去了. 我们都知道在web端的cookie是可以通过服务器端设置保存的,默认是关闭浏览器就清除cookie的,但是可以在服务器端设置coo ...

  3. Java7中的ForkJoin并发框架初探(下)—— ForkJoin的应用

    前两篇文章已经对Fork Join的设计和JDK中源码的简要分析.这篇文章,我们来简单地看看我们在开发中怎么对JDK提供的工具类进行应用,以提高我们的需求处理效率. Fork Join这东西确实用好了 ...

  4. ZooKeeper监听机制

    前言:Zookeeper的监听机制很多人都踩过坑,感觉实现了watcher 接口,后面节点的变化都会一一推送过来,然而并非如此. Watch机制官方声明:一个Watch事件是一个一次性的触发器,当被设 ...

  5. SpringData系列四 @Query注解及@Modifying注解

    @Query注解查询适用于所查询的数据无法通过关键字查询得到结果的查询.这种查询可以摆脱像关键字查询那样的约束,将查询直接在相应的接口方法中声明,结构更为清晰,这是Spring Data的特有实现. ...

  6. 安卓TextView限定行数最大值,点击按钮显示所有内容

    问题展示 如上图所示,在普通的TextView中,要求: 最多显示3行 超过三行显示展开按钮 且点击展开按钮显示完整内容 这个需求看似简单,但解决起来会遇到两个较为棘手的问题:1,如何判断是否填满了前 ...

  7. VR全景智慧城市

    随着虚拟现实产业的发展,我国对虚拟现实产业也越来越重视了.我国虚拟现实VR市场增长速度很快,市场活跃性很高,很多人都看好我国的虚拟现实VR市场,而且未来国内虚拟现实VR市场的销量还将有更大的增长.据赛 ...

  8. Python之向日志输出中添加上下文信息

    除了传递给日志记录函数的参数(如msg)外,有时候我们还想在日志输出中包含一些额外的上下文信息.比如,在一个网络应用中,可能希望在日志中记录客户端的特定信息,如:远程客户端的IP地址和用户名.这里我们 ...

  9. 005---query接口初步

    Query session.createQuery(String hql)方法; * hibernate的session.createQuery()方法是使用HQL(hibernate的查询语句)语句 ...

  10. 开涛spring3(6.4) - AOP 之 6.4 基于@AspectJ的AOP

    Spring除了支持Schema方式配置AOP,还支持注解方式:使用@AspectJ风格的切面声明. 6.4.1  启用对@AspectJ的支持 Spring默认不支持@AspectJ风格的切面声明, ...