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 ...
随机推荐
- c# Debug的一些技巧
c# Debug的一些技巧 专业工作也快两年,从最开始的F9,F10的断点调试,慢慢积累一些调试的技巧,令开发工作更加的效率 1.F9 最基础的断点, 点击F10 不跳入方法内部,点击F11逐行逐 ...
- C语言一些知识点总结
一.关键字 1. 什么是关键字 1> 关键字就是C语言提供的有特殊含义的符号,也叫做“保留字” 2> C语言一共提供了32个关键字,这些关键字都被C语言赋予了特殊含义 auto doubl ...
- 学习爬虫的day01
反扒 1.浏览器伪装加一个协议头(即浏览器的协议头) 火狐的浏览器协议头='User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; r ...
- display:box和display:flex填坑之路
背景分析:最近做移动端项目时,遇到一个常见的需求: 可以滑动的导航,如下图 虽然是很常见的一个布局,但在移动端没有做过,想当然的写下以下的样式,简单描述下: 父元素 width:100%: overf ...
- 使用Navicat导入.csv文件(过程和注意点)
1.创建一个数据库,右键点击表,选择导入向导. 2.在跳出的弹窗中选择.CSV文件,点击下一步 3.选择文件来源和编码规格,点击下一步 如果发现上传后中文出现乱码请使用10008这个编码规则 4.选择 ...
- [转] linux系统中如何进入退出vim编辑器,方法及区别
原文链接:http://www.cnblogs.com/crazylqy/p/5649860.html 在linux家族中,vim编辑器是系统自带的文本编辑器,其功能强大自不必说了. 偶有小白,刚接触 ...
- LVS+keepalived快速搭建测试环境
#LVS+keepalived快速搭建测试环境 #LVS+keepalived快速搭建测试环境 #centos6 X64 # LVS 负载均衡模式:DR(直接路由) 192.168.18.31 mas ...
- Laravel框架使用的一些注意细节(一)
1.资源路由RESTful 当你不想编写太多的路由的时候,肯定会用到RESTful资源控制器.但当你使用资源控制器的时候,需要注意的是,你的资源路由的名字不能与public目录下的文件有重名,否则会导 ...
- codeforces 893B Beautiful Divisors 打表
893B Beautiful Divisors 思路: 打表 代码: #include <bits/stdc++.h> using namespace std; #define _for( ...
- PHP面向对象-----魔术方法
PHP面向对象-----魔术方法 __get($name)--触发时机:当调用一个不访问的成员属性的时候,会自动触发,可以利用这个方法来完成对不可调用的属性进行调用,但是不能设置值 ___set($n ...