假设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. [原创]Java调用PageOffice在线打开数据库中保存的Word文件

    PageOffice产品和数据库是两个独立的概念,严格来说两者之间没有任何本质关系.PageOffice不依赖数据库而存在,但是数据库和PageOffice可以结合使用来完成某些复杂的业务逻辑.例如: ...

  2. Django之框架简介

    了解即可: 1.MVC,全名是Model View Controller,是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控制器(Controller ...

  3. springBoot 项目 jar/war打包 并运行

    一:idea  打jar  包 简述:springboor  项目最好的打包方式就是打成jar  ,下边就是简单的过程 1.打开idea工具 ,选着要打开的项目, 然后打开view--> too ...

  4. javascript 解析ajax返回的xml和json格式的数据

    写个例子,以备后用 一.JavaScript 解析返回的xml格式的数据: 1.javascript版本的ajax发送请求 (1).创建XMLHttpRequest对象,这个对象就是ajax请求的核心 ...

  5. [Ceoi2007]Royaltreasury

    #1945. [Ceoi2007]Royaltreasury Online Judge:Bzoj-1945 Label:树形Dp,高精度 题目描述 在很久很久以前的一个王国里,王国的财产开始变得越来越 ...

  6. 11.Hibernate一对多关系

    创建JavaBean 一方: Customer private long cust_id; private String cust_name; private long cust_user_id; p ...

  7. set_clock_latency

    set_clock_latancy用于定于虚拟时钟与真实时钟的延时 考虑最糟糕的情况,评估setup时数据会使用最大延时,时钟使用最小延时:评估hold时,数据使用最小延时,时钟使用最大延时.

  8. jqurey相册放大浏览效果。

    /*图片弹窗与切换*/ function honorLayer(){ var honorArray = honorArr(); var $msk = $('.js-mask'),$layer = $( ...

  9. 深入浅出 Java Concurrency (13): 锁机制 part 8 读写锁 (ReentrantReadWriteLock) (1)[转]

    从这一节开始介绍锁里面的最后一个工具:读写锁(ReadWriteLock). ReentrantLock 实现了标准的互斥操作,也就是一次只能有一个线程持有锁,也即所谓独占锁的概念.前面的章节中一直在 ...

  10. 开源代码分析-react-native-eyepetizer

    目录结构: app----imgs --- pages ------ home ------ explore ------ follow ------ profile ------  selected ...