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 ...
随机推荐
- Gotorch - 多机定时任务管理系统
* { color: #3e3e3e } body { font-family: "Helvetica Neue", Helvetica, "Hiragino Sans ...
- java 之 原型模式(大话设计模式)
原型模式,在笔者理解看来就是克隆,当我们在创建第一个对象时,已经给对象赋值完毕,此时我们需要一个当前对象的副本,如果没有原型模式,我们会再次创建一个对象,然后后二次赋值,保证两个对象完全一致, 这样我 ...
- JAVA 异常向上抛和向下抛的优劣势
向上抛: 优点:向上抛出异常,下面代码清秀: 缺点:不能直接看出抛出异常的代码: 向下抛: 优点:能直接看到出现异常的代码,方便查找,显得严谨: 缺点:代码太多: 总结:尽量向上抛,代码量减少,同意解 ...
- Java中的回调
又忙了一周,事情差不多解决了,终于有可以继续写我的博客了(各位看官久等了). 这次我们来谈一谈Java里的一个很有意思的东西--回调. 什么叫回调,一本正经的来讲,在计算机程序设计中,回调函数是指通过 ...
- 如何搭建一个B2B电商的跨境系统网站?
国内的B2B跨境电商系统开发定制如何做才符合标准?商家怎么搭建专属的电商供应链系统?目前并不是大多数电商行业的公司可以应对得起组建团队来做,下面分享下大概的建设供应链商城网站思路和步骤(以数商云跨境电 ...
- Android 异步消息处理机制终结篇 :深入理解 Looper、Handler、Message、MessageQueue四者关系
版权声明:本文出自汪磊的博客,转载请务必注明出处. 一.概述 我们知道更新UI操作我们需要在UI线程中操作,如果在子线程中更新UI会发生异常可能导致崩溃,但是在UI线程中进行耗时操作又会导致ANR,这 ...
- Spring Data 整合 ElasticSearch搜索服务器
一.基于 maven 导入坐标(pom.xml文件) <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi ...
- PHP根据传入参数合并多个JS和CSS文件的简单实现
HTML(使用方法): 复制代码代码如下: <link rel="stylesheet" type="text/css" href="cssmi ...
- Cesium中Clock控件及时间序列瓦片动态加载
前言 前面已经写了两篇博客介绍Cesium,一篇整体上简单介绍了Cesium如何上手,还有一篇介绍了如何将Cesium与分布式地理信息处理框架Geotrellis相结合.Cesium的强大之处也在于其 ...
- Java开发小技巧(一)
前言 相信许多程序员在看别人写的代码的时候,会有怀疑人生的感想,面对一堆天书一样的代码,很难摸清作者的思路,最后选择了重构,如果你认同上面这个作法,说明了两个问题:要么原来的开发者技术菜.要么你技术菜 ...