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. linux 下怎样查找一个文件夹在哪个目录下?

    如果只显示所在目录的路径: find 目录 -type d -name "查询目录名" -printf "%h\n" 如果同时显示目录名称和所在目录的路径: f ...

  2. 纳税服务系统【用户模块之使用POI导入excel、导出excel】

    前言 再次回到我们的用户模块上,我们发现还有两个功能没有完成: 对于将网页中的数据导入或导出到excel文件中,我们是完全没有学习过的.但是呢,在Java中操作excel是相对常用的,因此也有组件供我 ...

  3. 初学者---AngularJS详解

    AngularJS 简介 AngularJs是一个用于设计动态web应用的结构框架.首先,它是一个框架,不是类库,提供一整套方案用于设计web应用.它不仅仅是一个javascript框架,因为它的核心 ...

  4. MapReduce执行过程

    Mapper任务的执行过程: 第一阶段是把输入文件按照一定的标准分片(InputSplit),每个输入片的大小是固定的.默认情况下,输入片(InputSplit)的大小与数据块(Block)的大小是相 ...

  5. OC——继承

    继承的其中一个很重要的目的是为了实现多态.我们现在先来看看OC的继承. 一.继承 父类: 头文件 // // Peason.h // 01-继承和多态 // // Created by zhangji ...

  6. canvas浅谈 实现简单的自旋转下落

    旋转和平移是2个基础的动画效果,也是复杂动画的基础. 如果是普通的页面只要设置transform属性很容易实现平移+旋转的组合效果,达到自旋转下落的效果.因为操作的直接是动作元素本身很容易理解. 但是 ...

  7. js'初学笔记

    之前看过一个博主说的学习前端养成写博客的习惯,我慢慢学着在上面写点东西,记录我的学习. 这段时间把之前学的js基础补上一点,学了一些对数组和字符的操作,split(),将字符串变成数组.join(), ...

  8. Opengl4.5 中文手册—B

    索引 A      B    C      D     E     F     G H      I     J      K     L     M     N O      P    Q      ...

  9. 每天学点SpringMVC-异常处理

    1. 第一步先写个Hello World 1.1 编写一个抛出异常的目标方法 @RequestMapping("/testException.do") public String ...

  10. IE兼容

    这个基本知识http://www.cnblogs.com/yoosou/archive/2012/07/27/2612443.html 参考: http://www.cnblogs.com/cocow ...