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 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:
- The length of both lists will be in the range of [1, 1000].
- The length of strings in both lists will be in the range of [1, 30].
- The index is starting from 0 to the list length minus 1.
- 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里找到相同的并且位置总和最靠前的)的更多相关文章
- 【Leetcode_easy】599. Minimum Index Sum of Two Lists
problem 599. Minimum Index Sum of Two Lists 题意:给出两个字符串数组,找到坐标位置之和最小的相同的字符串. 计算两个的坐标之和,如果与最小坐标和sum相同, ...
- 【LeetCode】599. Minimum Index Sum of Two Lists 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:找到公共元素再求索引和 方法二:索引求和,使 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- LeetCode 599: 两个列表的最小索引总和 Minimum Index Sum of Two Lists
题目: 假设 Andy 和 Doris 想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. Suppose Andy and Doris want to cho ...
- [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 ...
随机推荐
- Spring Security研究(1)
1, 获取Spring Security的Jar包 :从Spring网站下载页下载或者从Maven中央仓库下载.一个好办法是参考实例应用中包含的依赖库. 2,项目模块: Core - spring ...
- JSP页面格式化数字或时间 基于struts的
jsp日期格式化 转自: http://blog.csdn.net/chj225500/article/details/7251552 在直接<s:textfield中也要日期格式化,平时使用日 ...
- grunt学习笔记1 理论知识
你需要检查js语法错误,然后再去压缩js代码.如果这两步你都去手动操作,会耗费很多成本.Grunt就能让你省去这些手动操作的成本. “—save-dev”的意思是,在当前目录安装grunt的同时,顺便 ...
- CentOS7安装后配置MariaDB
安装后,优先推荐先对安全设置进行配置,键入命令 sudo mysql_secure_installation 键入当前密码,当前没有,直接回车,之后跟随提示会问几个问题:设置 root 密码? / 移 ...
- MMORPG战斗系统随笔(一)
前言 很久没有更新博客,中间迁移过一次博客,后来一直忙于项目的开发,忙的晚上回去没时间写博客,周日又要自我调整一下,所以空闲了很久没有继续写博客.最近终于慢慢放慢节奏,项目也快上线了,可以有空写一些个 ...
- Object.defineProperty()方法的用法详解
Object.defineProperty()函数是给对象设置属性的. Object.defineProperty(object, propertyname, descriptor); 一共有三个参数 ...
- C++PrimerPlus第6版 第四章——复合类型
1,复合类型主要包含:数组.结构.联合.枚举.类.指针.引用等. 2,数组.长度必须确定.即编译阶段,数组的长度就得确定好.所以只能使用常量(#define.const)声明数组长度.如果使用变量声明 ...
- SQLite中的时间日期函数
SQLite包含了如下时间/日期函数: datetime().......................产生日期和时间 date()...........................产生日期 t ...
- Linux文件类型介绍
文件类型介绍: Linux系统不同于Windows系统,两者文件类型和文件扩展名也有很大的差异.Linux中的文件类型和Linux文件的文件扩展名所代表的意义和Windows系统完全不同.用户一般通过 ...
- Echarts数据可视化series-graph关系图,开发全解+完美注释
全栈工程师开发手册 (作者:栾鹏) Echarts数据可视化开发代码注释全解 Echarts数据可视化开发参数配置全解 6大公共组件详解(点击进入): title详解. tooltip详解.toolb ...