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的更多相关文章

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

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

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

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

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

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

  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如何在日志中查找关键字、前几行、结尾几行

    如何使用命令行快速查看项目日志是每个开发人员必备技能,尤其在没有专门日志搜集系统的情况下,想要知道目前项目运行状态最好的办法就是打开log日志一瞅即明白. 复杂的到用时再查不晚,但是简单的还是有必要掌 ...

  2. 通过ELK快速搭建一个你可能需要的集中化日志平台

    在项目初期的时候,大家都是赶着上线,一般来说对日志没有过多的考虑,当然日志量也不大,所以用log4net就够了,随着应用的越来越多,日志散 落在各个服务器的logs文件夹下,确实有点不大方便,这个时候 ...

  3. 机器学习算法--Perceptron(感知机)算法

    感知机: 假设输入空间是\(\chi\subseteq R^n\),输出空间是\(\gamma =\left( +1,-1\right)\).输入\(\chi\in X\)表示实例的特征向量,对应于输 ...

  4. tnsping非常慢

    最近给同事虚拟机上安装了一个11g数据库,发现一个奇怪的问题,用windows客户段连接时候非常慢,慢到不能容忍的地步,但是本地os验证登录没有问题,速度非常快,初步定为问题出在监听上,于是我tnsp ...

  5. 用lua+redis实现一个简单的计数器功能 (一)

    首先安装环境 依赖环境有 luajit http://luajit.org ngx_devel_kit https://github.com/simpl/ngx_devel_kit echo-ngin ...

  6. cinder存储节点 后端采用lvm、nfs安装配置

    #cinder存储节点 openstack pike 部署 目录汇总 http://www.cnblogs.com/elvi/p/7613861.html #cinder存储节点 #cinder后端采 ...

  7. LINQ学习系列-----3.1 查询非泛型集合

    一.问题起源 LINQ to object在设计时,是配合IEnumerable<T>接口的泛型集合类型使用的,例如字典.数组.List<T>等,但是对于继承了IEnumera ...

  8. PhpStorm连接服务器,开始自动上传功能

    连接服务器 菜单栏找到[工具/Tools]->[Deployment/部署]->[Confinguration-/配置-]. 点加号(+),添加一台服务器,填写名称,选择类型为SFTP,点 ...

  9. PHP生成xml 无法识别或是无法读取或是浏览器不识别等问题

    PHP 数组转XML函数如下 [PHP] 纯文本查看 复制代码 ? 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 ...

  10. 数据库索引------Hash索引的使用限制

    1.hash索引必须进行二次查找. 2.hash索引无法进行排序. 3.hash索引不支持部分索引查找也不支持范围查找. 4.hash索引中hash码的计算可能存在hash冲突.