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

题目:

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.

题解:

把list1的element 和对应的index放进HashMap<String, Integer> hm中. 再iterate list2, 如果list2的element在hm中比较index相加是否比minIndexSum小,若比minIndexSum小,清空res重新加, 更新minIndexSum. 若相等直接加进res中.

Time Complexity: O(list1.length + list2.length)

Space: O(list1.length).

AC Java:

 class Solution {
public String[] findRestaurant(String[] list1, String[] list2) {
List<String> res = new ArrayList<String>();
int minIndexSum = Integer.MAX_VALUE;
HashMap<String, Integer> hm = new HashMap<String, Integer>();
for(int i = 0; i<list1.length; i++){
hm.put(list1[i], i);
} for(int j = 0; j<list2.length; j++){
if(hm.containsKey(list2[j])){
int i = hm.get(list2[j]);
if(i+j < minIndexSum){
res.clear();
res.add(list2[j]);
minIndexSum = i+j;
}else if(i+j == minIndexSum){
res.add(list2[j]);
}
}
} return res.toArray(new String[res.size()]);
}
}

LeetCode Minimum Index Sum of Two Lists的更多相关文章

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

  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 (从两个lists里找到相同的并且位置总和最靠前的)

    Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite ...

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

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

  5. 【LeetCode】599. Minimum Index Sum of Two Lists 解题报告(Python)

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

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

  7. C#LeetCode刷题之#599-两个列表的最小索引总和​​​​​​​​​​​​​​(Minimum Index Sum of Two Lists)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3802 访问. 假设Andy和Doris想在晚餐时选择一家餐厅,并 ...

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

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

随机推荐

  1. 动手动脑:String.equals()的使用方法

    public class StringEquals { /** * @param args the command line arguments */ public static void main( ...

  2. json教程系列(1)-使用json所要用到的jar包下载

    json是个非常重要的数据结构,在web开发中应用十分广泛.我觉得每个人都应该好好的去研究一下json的底层实现,基于这样的认识,金丝燕网推出了一个关于json的系列教程,分析一下json的相关内容, ...

  3. springboot-数据库

    Spring-data-jpa jpa定义了一系列持久化的标准,比如hibernate就实现了这一标准. Springboot 的jpa就是hibernate的整合. 在pom文件中增加配置: < ...

  4. 解决Ubuntun 12.04编译Mesa10.3 WARNING: 'aclocal-1.14' is missing on your system

    安 装Mesa时,最后一个错误报“WARNING: 'aclocal-1.14' is missing on your system.”,虽然是个Warning,但是无法进行下一步make,所以必须解 ...

  5. Django基础知识MTV

    Django简介 Django是使用Python编写的一个开源Web框架.可以用它来快速搭建一个高性能的网站. Django也是一个MVC框架.但是在Django中,控制器接受用户输入的部分由框架自行 ...

  6. MongoDB命令语法小用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using MongoDB; ...

  7. poj 3617输出格式问题

    注意是说的80个字母一行....

  8. 高通平台MSM8916LCM模块移植(一)-bootloader部分【转】

    本文转载自:http://www.mobile-open.com/2016/970947.html 高通平台中的bootloader叫做LK(Little Kernel,对于LCM来说LK部分相当重要 ...

  9. Linux mysql主从同步配置

    一.在两台Ubuntu机器上安装mysql1.检查系统中是否安装了mysql 这个是已经安装了的 没有安装的话执行上条命令===============================MySQL的一些 ...

  10. freemarker 异常处理

    SSH2处理方案: freemarker文件如果出错,网站的前台页面会报出很明显的错误-焦黄的背景,血红的文字,很不利于用户体验的.如何修改这个问题呢?首先需要在struts.xml配置文件里添加下面 ...