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.

题目标签:Hash Table

  这道题让我们从两个list中找出相同的string并且它们的index sum是最小的,意思就是让我们找到他们两个都想吃的同一个饭店,并且这个饭店是他们排位里最靠前的。

Step 1: 首先我们建立一个HashMap, 遍历list1, 把饭店string作为key, 把index作为value保存进map。

Step 2: 建立一个HashSet, 遍历list2, 如果找到list2里的饭店string是在之前的map里的话,更新一下map的value = index1(之前的value) + index2(在list2里的);并且设一个min,在记录最小的index sum,把最小的饭店string保存进set里面。

    当找到一个相同的index sum = min的话,把这个饭店string加入set;当找到更小的min的时候,要把set清空,因为出现更小的index sum的饭店了,淘汰前面的饭店,重设min的值。

Step 3: 此时set里的饭店名就是最靠前的并且是两个list都有的,设置一个string array把答案copy进去return。

   

Java Solution:

Runtime beats 69.17%

完成日期:06/07/2017

 public class Solution
{
public String[] findRestaurant(String[] list1, String[] list2)
{
HashMap<String, Integer> map = new HashMap<>(); // put list1's string as key, index as value.
for(int i=0; i<list1.length; i++)
map.put(list1[i], i); HashSet<String> set = new HashSet<>(); int min = Integer.MAX_VALUE; // iterate list2 to see any string is in map
for(int i=0; i<list2.length; i++)
{
if(map.get(list2[i]) != null) // if list2's string is in map
{
int j = map.get(list2[i]);
map.put(list2[i], i + j); // update map's value if(i+j == min) // if find another same min value, add this string to set.
set.add(list2[i]);
else if(i+j < min) // if find another smaller index
{
set.clear(); // clear the set.
set.add(list2[i]); // add smaller index string into set.
min = i+j; // update the min;
} }
} String[] res = new String[set.size()]; int i=0;
for(String s : set)
{
res[i] = s;
i++;
} return res;
}
}

参考资料:

http://blog.csdn.net/kangbin825/article/details/72794253

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 599. Minimum Index Sum of Two Lists (从两个lists里找到相同的并且位置总和最靠前的)的更多相关文章

  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 解题报告(Python)

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

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

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

  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. java.sql.Exception:setString 只能处理少于 32766 个字符的字符串

    java.sql.Exception:setString 只能处理少于 32766 个字符的字符串 解决方式是 : 升级ojdbc的版本,   将原来的 ojdbc14_10.2.0.2.0.jar ...

  2. mysql数据库-注释相关介绍

    mysql执行的sql脚本中注释怎么写? mysql 服务器支持 # 到该行结束.-- 到该行结束 以及 /* 行中间或多个行 */ 的注释方格: mysql; # 这个注释直到该行结束 mysql; ...

  3. Windows下chm转换为html的超简单方法

    摘要:通过调用Windows命令,将chm 文件转换为html 文件 概述:很多程序员朋友都会遇到这样的问题,看一个离线版的帮助文档(chm文件),总会产生一个索引文件(该文件的chw文件), 而且有 ...

  4. AngularJS的运用

      前  言 JRedu AngularJS[1]  诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前端JS框架,已经被用于Google的多款产品当中.A ...

  5. 记录一次无聊的(经历了Nodejs -> Shell -> C)的探索问题过程

    提出问题 在运行项目的服务器的git是1.8.3.1版本的时候,pm2 deploy 项目,服务器fetch不到最新的一次commit. 对于这个问题,在pm2的github也有issues讨论.然后 ...

  6. react基础(2)

    react基础(1):介绍了如何创建项目,总结了JSX.组件.样式.事件.state.props.refs.map循环,另外还讲了mock数据和ajax 还是用 react基础1 里创建的项目继续写案 ...

  7. TComboBox组件重要属性和事件

    TComboBox组件的重要属性 CharCase--------此属性用于设置编辑框内文字的大小写 DropDownCount---此属性用于设置当用户下拉组合框时不需要加滚动条就能显示的项的个数 ...

  8. Win下安装虚拟机(Linux)

    **********************win下体验linux**************************************By熟知宇某 一.先说说win10和win8系统下的hyp ...

  9. cmd获取python返回值

    test.py代码如下: import urllib2 import sys try: f = urllib2.urlopen('http://www.baidu.com/',timeout = 10 ...

  10. myeclipse 中clean的作用

    myeclipse中clean的作用     重新编译的功能 就是将编译好的class文件都删除后在重新生成.如果引用的jar包不能工作可以尝试下.