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 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.
题目的大意是找到两个数组中相同元素的index和最小的元素。解法是将数组a的元素放入map中,值为key、index为value, 当数组b也出现同样的元素的时候,两个元素的index的和与当前的最小值判断,大就舍弃,小就更新最小值,并放入一个新的列表。
public static String[] findRestaurant(String[] list1, String[] list2) {
int min = Integer.MAX_VALUE;
List<String> result = new ArrayList<>();
HashMap<String,Integer> map = new HashMap<>();
for(int i = 0; i < list1.length; i++)
map.put(list1[i],i);
for(int i = 0; i < list2.length; i++)
{
if(map.containsKey(list2[i]))
{
int index = map.get(list2[i]);
if(i + index == min)
{
result.add(list2[i]);
}
else if(i + index < min)
{
result.clear();
result.add(list2[i]);
}
}
}
return result.toArray(new String[result.size()]); //这里将list转为数值
}
看了leetcode上面的Discuss其他用户的写法,上面的代码可以做一个小小的优化,
public static String[] findRestaurant(String[] list1, String[] list2) {
int min = Integer.MAX_VALUE;
List<String> result = new ArrayList<>();
HashMap<String,Integer> map = new HashMap<>();
for(int i = 0; i < list1.length; i++)
map.put(list1[i],i);
for(int i = 0; i < list2.length; i++)
{
Integer index = map.get(list2[i]); /////
if(index != null && i + index <= min)
{
if(i + index < min)
{
result.clear(); //存在更小的值,就将列表清空
min = i + index;
}
result.add(list2[i]);
}
}
return result.toArray(new String[result.size()]); //这里将list转为数值
}
这是一个常规的题目,主要是需要注意的有以下几点
- 在选择较小的值的时候,往往为min赋值一个最大的值,注意写法
- 判断map是否包含某个key使用
containsKey返回的为布尔类型,可以灵活使用Integer - 清空列表使用
clear,注意列表转数组的用法,数组转列表使用Arrays.asList(a)
599. Minimum Index Sum of Two 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 (从两个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 ...
- [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 ...
- 【LeetCode】599. Minimum Index Sum of Two Lists 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:找到公共元素再求索引和 方法二:索引求和,使 ...
- 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 ...
随机推荐
- JSP7(Cookie与javamail)
一.cookie是什么意思? 英文直接翻译过来的意思呢就是小甜品 Cookie英文意指饼干,不过在电脑术语中它可不像饼干那么简单.简单的说,Cookie就是服务器暂存放在你计算机上的一笔资料,好让服务 ...
- Numpy入门 - 数组排序
本节主要讲解numpy数组的排序方法sort的应用,包括按升序排列和按降序排列. 一.按升序排列 import numpy as np arr = np.array([[3, 1, 2], [6, 4 ...
- Smarty基础用法
一.Smarty基础用法: 1.基础用法如下 include './smarty/Smarty.class.php';//引入smarty类 $smarty = new Smarty();//实例化s ...
- 如果nginx启动失败,错误解决
解决上面问题: /usr/sbin/groupadd -f www /usr/sbin/useradd -g www www 这方法常见出现时反向代理时,ssl的授权用户不存在的情况下出现的:.
- 深入常用CSS声明(一) —— Background
一直对一些自己常用的css声明掌握得不是很全,只知道常用的一些属性和值,但是对于其他的用法确实一知半解,这篇文章旨在扫盲,先不说有多深的理解,至少做到能够看到这些声明的属性和值的时候做到不陌生. 这里 ...
- 开源API测试工具 Hitchhiker v0.6更新 - 改进压力测试
Hitchhiker 是一款开源的支持多人协作的 Restful Api 测试工具,支持Schedule, 数据对比,压力测试,支持上传脚本定制请求,可以轻松部署到本地,和你的team成员一起协作测试 ...
- IQKeyboardManager 状态栏(status bar)问题
因为懒,所以具体什么样子,参考下面的链接 具体的问题情况参考:StatusBar background problem #1158 我解决的思路很简单,就是在监听键盘消失的时候,去设置 statys ...
- Java 封装 HDFS API 操作
代码下载地址:点击下载 一:环境介绍 hadoop:2.6 Ubuntu:15.10 eclipse:3.8.1 二:操作包含 推断某个目录是否存在 isExist(fold ...
- ListView原理
表明转载自http://blog.csdn.net/iispring/article/details/50967445 在自己定义Adapter时,我们经常会重写Adapter的getView方法,该 ...
- LeetCode(24) Swap Nodes in Pairs
题目 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-> ...