Leetcode599.Minimum Index Sum of Two Lists
假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示。
你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅。 如果答案不止一个,则输出所有答案并且不考虑顺序。 你可以假设总是存在一个答案。
示例 1:
输入: ["Shogun", "Tapioca Express", "Burger King", "KFC"] ["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"] 输出: ["Shogun"] 解释: 他们唯一共同喜爱的餐厅是“Shogun”。
示例 2:
输入: ["Shogun", "Tapioca Express", "Burger King", "KFC"] ["KFC", "Shogun", "Burger King"] 输出: ["Shogun"] 解释: 他们共同喜爱且具有最小索引和的餐厅是“Shogun”,它有最小的索引和1(0+1)。
提示:
- 两个列表的长度范围都在 [1, 1000]内。
- 两个列表中的字符串的长度将在[1,30]的范围内。
- 下标从0开始,到列表的长度减1。
- 两个列表都没有重复的元素。
把 ‘=’ 写成了 ‘==’
class Solution {
public:
vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
map<string, int> hax;
int len1 = list1.size();
int len2 = list2.size();
for(int i = 0; i < len1; i++)
{
hax[list1[i]] = i + 1;
}
vector<string> res;
int MIN = INT_MAX;
for(int i = 0; i < len2; i++)
{
if(hax[list2[i]] > 0 && i + hax[list2[i]] - 1 < MIN)
{
res.clear();
MIN = i + hax[list2[i]] - 1;
}
if(hax[list2[i]] > 0 && i + hax[list2[i]] - 1 == MIN)
{
res.push_back(list2[i]);
}
}
return res;
}
};
Leetcode599.Minimum Index Sum of Two Lists的更多相关文章
- 【Leetcode_easy】599. Minimum Index Sum of Two Lists
problem 599. Minimum Index Sum of Two Lists 题意:给出两个字符串数组,找到坐标位置之和最小的相同的字符串. 计算两个的坐标之和,如果与最小坐标和sum相同, ...
- [Swift]LeetCode599. 两个列表的最小索引总和 | 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 ...
- 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 ...
- [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 ...
- 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 ...
- LeetCode Minimum Index Sum of Two Lists
原题链接在这里:https://leetcode.com/problems/minimum-index-sum-of-two-lists/description/ 题目: Suppose Andy a ...
- LeetCode 599: 两个列表的最小索引总和 Minimum Index Sum of Two Lists
题目: 假设 Andy 和 Doris 想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. Suppose Andy and Doris want to cho ...
随机推荐
- windows环境下,svn未备份情况下重新恢复
公司有个同事在未打招呼的情况下把公司服务器进行重新装系统,崩溃啊.SVN之前未备份,还好SVN的库(Repositories)还在,如下图: 恢复办法如下: 由于之前安装的就是VisualSVN-Se ...
- csdn阅读更多自动展开插件
点击获取 当然也可以自己写脚本.写js.
- 深入理解JVM(一)类加载器部分、类变量、常量、jvm参数
类加载概述 在java代码中,类型的加载.连接与初始化过程都是在程序运行期间完成的 类型:class.interface(object本身).类型可在运行期间生成,如动态代理.一种runting概念 ...
- Spring MVC(四)--控制器接受pojo参数
以pojo的方式传递参数适用于参数较多的情况,或者是传递对象的这种情况,比如要创建一个用户,用户有十多个属性,此时就可以通过用户的pojo对象来传参数,需要注意的是前端各字段的名称和pojo对应的属性 ...
- 关于MQ 消息队列的通俗理解和 rabbitMQ 使用
消息队列,一听很高大上,现在很多分布式系统都在用这个消息中间件 网上一搜, 说的都是些原理. 说下我的通俗理解, 你网上买了, 快递员给你投递, 会出现什么问题呢? 1 你不定时在家, 快递员 来了 ...
- CentOS6安装docker、docker-compose、docker-enter
一.安装docker 1.查看CentOS内核版本 uname -r 2.安装Fedora的EPEL源 yum install http://ftp.riken.jp/Linux/fedora/epe ...
- Mybatis和spingboot整合
0. 导包 <!-- 统一管理springboot相关的包 --> <parent> <groupId>org.springframework.boot</g ...
- Python GUI文本编辑器
使用Python编写一个简单的文本编辑器,需要展示一个用户界面,功能包括打开.保存文本文件. 使用tkinter库来编写GUI. #简单GUI文本编辑器 from tkinter import * f ...
- RocketMQ补偿方案架构设计
RocketMQ作为消息中间件,在系统异步化架构中,应用非常广泛.但是我们在享用RocketMQ的同时,也不能百分百完全信赖它.一旦RocketMQ崩溃了,给我们业务带来的也将是毁灭性打击. 因此,我 ...
- [JLOI2015]战争调度【暴力+树形Dp】
Online Judge:Bzoj4007,Luogu P3262 Label:暴力,树形Dp 题解 参考了这篇blog https://www.cnblogs.com/GXZlegend/p/830 ...