假设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. 两个列表的长度范围都在 [1, 1000]内。
  2. 两个列表中的字符串的长度将在[1,30]的范围内。
  3. 下标从0开始,到列表的长度减1。
  4. 两个列表都没有重复的元素。

把 ‘=’ 写成了 ‘==’

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的更多相关文章

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

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

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

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

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

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

  8. LeetCode Minimum Index Sum of Two Lists

    原题链接在这里:https://leetcode.com/problems/minimum-index-sum-of-two-lists/description/ 题目: Suppose Andy a ...

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

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

随机推荐

  1. NSLayoutConstraint 开源框架

    https://github.com/cloudkite/Masonry Masonry is a light-weight layout framework which wraps AutoLayo ...

  2. L2-006 树的遍历 (层序遍历)

    根据访问根节点与左右子树的先后顺序,二叉树一般有三种遍历方式:先序遍历.中序遍历和后序遍历. 只要给定中序遍历序列与先序或后序中的一种,可以还原二叉树结构.学习数据结构课程时,一直都只会手动构建还原二 ...

  3. webGL动画

    在做这个项目之前,我也和很多人的想法一样觉得:H5做动画性能不行,只能完成简单动画,可是事实并非如此.所以借此篇分享振奋下想在H5下做酷炫游戏的人心. 体验游戏请长按二维码识别: 好吧,知道你懒.不想 ...

  4. 【流畅的python】16 - 协程

    yield 产生是产生值给调用方 让步是暂停生成器,同时让步也可以作为流程控制手段 yield item 上面这行代码会产出一个值.提供给next(...)的调用方.此外还会做出让步,暂停执行生成器, ...

  5. 最新二进制安装部署kubernetes1.15.6集群---超详细教程

    00.组件版本和配置策略 00-01.组件版本 Kubernetes 1.15.6 Docker docker-ce-18.06.1.ce-3.el7 Etcd v3.3.13 Flanneld v0 ...

  6. matlab-选择-循环-函数

    1 选择 3 循环 break 3 函数

  7. Linux常用技巧

    1.解决不能中文显示 xshell 终端语言显示选择UTF-8 #yum groupinstall chinese-support 2.heredocument报错“unexpected end of ...

  8. 2019-8-26-当-ASP.NET-Core-链接找不到时可能的原因

    title author date CreateTime categories 当 ASP.NET Core 链接找不到时可能的原因 lindexi 2019-08-26 18:52:28 +0800 ...

  9. Vue配置多个跨域目标链接

    参考: https://segmentfault.com/a/1190000016199721 1.通过使用的http-proxy-middleware来实现跨域代理 devServer: { dis ...

  10. tensorflow+inceptionv3图像分类网络结构的解析与代码实现

    tensorflow+inceptionv3图像分类网络结构的解析与代码实现 论文链接:论文地址 ResNet传送门:Resnet-cifar10 DenseNet传送门:DenseNet SegNe ...