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:

  1. The length of both lists will be in the range of [1, 1000].
  2. The length of strings in both lists will be in the range of [1, 30].
  3. The index is starting from 0 to the list length minus 1.
  4. No duplicates in both lists.
/*
暴力的话O(n^2),可以利用hashtable,用空间来换时间。
*/
class Solution {
public:
vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
vector<string> res;
map<string, int> data;
int minval = INT_MAX;
for (int i = ; i < list1.size(); i++){ // 存入hashtable中
data.insert(pair<string, int>(list1[i], i));
}
for (int i = ; i < list2.size(); i++){
if (data.find(list2[i]) != data.end()){
if (data[list2[i]] + i < minval){
minval = data[list2[i]] + i;
res.clear();
res.push_back(list2[i]);
}else if (data[list2[i]] + i == minval){
res.push_back(list2[i]);
}
}
}
return res;
}
};

599. Minimum Index Sum of Two Lists(easy)的更多相关文章

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

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

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

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

  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. 【LeetCode】599. Minimum Index Sum of Two Lists 解题报告(Python)

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

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

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

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

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

  9. [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. Paypal 支付功能的 C# .NET / JS 实现

    说明 最近用到了 Paypal 支付功能,英语一般般的我也不得不硬着头皮踩一踩这样的坑.经过近乎半个月的作,终于实现了简单的支付功能,那么首先就说说使用 Paypal 必定要知道的几点(当前日期 20 ...

  2. 使用顶级 VSCode 扩展来加快开发 JavaScript

    使用顶级 VSCode 扩展来加快开发 JavaScript 发表于 2018年08月24日 by 愚人码头 被浏览 3,942 次 分享到:   小编推荐:掘金是一个面向程序员的高质量技术社区,从 ...

  3. 杂牌机搞机之旅(一)——获得root权限(刷入magisk)

    刷机不规范,抱机两行泪,谨慎刷机!! 一般获取root权限,我们都是通过软件来获取的,但是,软件破解root的成功率不是很高,现在,android版本普遍5.0+,大名鼎鼎的magisk可以直接获得r ...

  4. 大型网站架构演进(6)使用NoSQL和搜索引擎

    随着网站业务越来越复杂,对数据存储和检索的需求也越来越复杂,网站需要采用一些非关系型数据库技术(即NoSQL)和非数据库查询技术如搜索引擎.NoSQL数据库一般使用MongoDb,搜索引擎一般使用El ...

  5. 设计模式之解释器模式——Java语言描述

    解释器模式提供了评估语言的语法或表达式的方式,它属于行为型模式.这种模式实现了一个表达式接口,该接口解释一个特定的上下文.这种模式被用在SQL解析.符号处理引擎等 介绍 意图 给定一个语言,定义它的文 ...

  6. JavaScript 包装对象

    万物皆对象 在JavaScript里,万物皆对象.但是某些对象有别于其它对象,我们可以用 typeof 来获取一个对象的类型,它总是返回一个字符串. typeof 123; // 'number' t ...

  7. 让priority_queue支持小根堆的几种方法

    点击这里了解什么是priority_queue 前言 priority_queue默认是大根堆,也就是大的元素会放在前面 例如 #include<iostream> #include< ...

  8. ArcGIS for JavaScript学习(二)Server发布服务

    一 ArcGIS for Server 安装.配置 (1)双击setup (2)点击下一步完成安装 (3)配置 a 登录Manager 开始—>程序—>ArcGIS—>Manager ...

  9. 版本控制工具(SVN/Git)介绍

    文章大纲 一.SVN介绍二.Git介绍三.IDEA使用SVN和Git四.总结五.参考文章   一.SVN介绍 1. SVN服务器搭建和使用 首先来下载和搭建SVN服务器,下载地址如下: http:// ...

  10. FIDDLER的使用方法及技巧总结

    转自: https://www.cnblogs.com/ink-marks/p/6363275.html 一.FIDDLER快速入门及使用场景 Fiddler的官方网站:http://www.fidd ...