题目:

假设 Andy 和 Doris 想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示。

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.

示例 1:

输入:
["Shogun", "Tapioca Express", "Burger King", "KFC"]
["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"]
输出: ["Shogun"]
解释: 他们唯一共同喜爱的餐厅是“Shogun”。

示例 2:

输入:
["Shogun", "Tapioca Express", "Burger King", "KFC"]
["KFC", "Shogun", "Burger King"]
输出: ["Shogun"]
解释: 他们共同喜爱且具有最小索引和的餐厅是“Shogun”,它有最小的索引和1(0+1)。

提示:

  1. 两个列表的长度范围都在 [1, 1000] 内。
  2. 两个列表中的字符串的长度将在 [1,30] 的范围内。
  3. 下标从 0 开始,到列表的长度减 1。
  4. 两个列表都没有重复的元素。

Note:

  1. The length of both lists will be in the range of [1, 1000].
  2. The length of strings in both lists will be in the range of [1, 30].
  3. The index is starting from 0 to the list length minus 1.
  4. No duplicates in both lists.

解题思路:

两个字符串数组,找重复出现的元素,返回其索引和最小的目标数组。最容易想到的解法就是用哈希映射解题,Key 存储其数组的每个元素值,Value 存储其下标索引。第一次遍历将其中一个数组添加到哈希映射,第二次遍历查找目标元素。需要维护一个最小索引和来保证查询的目标索引和为最小。

哈希表解题:

Java:

class Solution {
public String[] findRestaurant(String[] list1, String[] list2) {
Map<String, Integer> map = new HashMap<>();//建立哈希映射
for (int i = 0; i < list1.length; i++)//初次遍历将一个数组建立映射关系
map.put(list1[i], i);
List<String> res = new ArrayList<>();//待返回的目标数组
int sum = Integer.MAX_VALUE;//sum为当前满足条件的最小索引和
for (int i = 0; i < list2.length; i++) {//第二次遍历查找目标元素
if (map.containsKey(list2[i])) {
int tmp = i + map.get(list2[i]);//当前索引和
if (tmp < sum) {//如果当前索引和更小
res.clear();//清除目标数组
res.add(list2[i]);//添加该元素
sum = tmp;// 刷新最小索引和
} else if (tmp == sum)//如果索引和相等
res.add(list2[i]);//只添加元素
}
}
return res.toArray(new String[res.size()]);//转成 string 数组
}
}

Python:

class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
hash_map = dict()# 建立哈希映射
for i, s in enumerate(list1):# 初次遍历将一个数组建立映射关系
hash_map[s] = i
min_sum = 2001# 当前满足条件的最小索引和
res = list()# 待返回的目标数组
for i, s in enumerate(list2):# 第二次枚举遍历查找目标元素
if s in hash_map:
tmp = i+hash_map[s]# 当前索引和
if tmp < min_sum:# 如果当前索引和更小
res.clear()# 清除目标数组
res.append(s)# 添加该元素
min_sum = tmp# 刷新最小索引和
elif tmp == min_sum:# 如果索引和相等
res.append(s)# 只添加元素
return res

操作索引解题:

这种解法非常巧妙,虽然效率很低。以下解释摘自 LeetCode,可以作为参考扩展思路:

另一种可以遍历不同 sumsum (下标和),并判断是否有字符串分别出现在 list1 和 list2 中且下标和为 sum。

现在我们知道下标和的值 sum 数值范围从 0 到 m + n - 1。这里 m 和 n 分别是 list1 和 list2 的长度,我们现在可以升序枚举 sum ,对于每个 sum,我们遍历 list1,假设当前下标为 i,为了得到下标和 sum,list2 中的下标 j 为 sum−i。通过这样的办法,我们不需要遍历 list2,而可以直接通过计算得到在 list2 中对应的下标。

对于每个 sum,我们遍历 list1 的所有下标,一旦有 list1 和 list2 中的字符串匹配,就把匹配字符串放入一个 res 列表中。

我们对 sum 升序数组中所有值做相同的过程,对于每个 sum 遍历完一遍 list1 之后,我们检查 res 列表是否为空。如果是空的,我们继续遍历下一个 sum 数组。如果不为空,当前的 res 就是最小下标和的数组。这是因为我们遍历 sum 的顺序是升序的,所以第一个找到的列表就是结果列表。

!--链接:https://leetcode-cn.com/problems/minimum-index-sum-of-two-lists/solution/liang-ge-lie-biao-de-zui-xiao-suo-yin-zong-he-by-l/--

Java:

class Solution {
public String[] findRestaurant(String[] list1, String[] list2) {
List<String> res = new ArrayList<>();
for (int sum = 0; sum < list1.length + list2.length - 1; sum++) {
for (int i = 0; i <= sum; i++) {
if (i < list1.length && sum - i < list2.length && list1[i].equals(list2[sum - i]))
res.add(list1[i]);
}
if (res.size() > 0) break;//一旦找到最小索引和序列直接结束遍历,因为sum是递增的,之后得到的索引和一定更大
}
return res.toArray(new String[res.size()]);
}
}

Python

class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
res = list()
list1_size, list2_size = len(list1), len(list2)
for min_sum in range(list1_size+list2_size-1):
for i in range(min_sum+1):
if i < list1_size and min_sum-i < list2_size and list1[i] == list2[min_sum-i]:
res.append(list1[i])
if len(res) > 0:# 一旦找到最小索引和序列直接结束遍历,因为sum是递增的,之后得到的索引和一定更大
break
return res

欢迎关注微。信公。众号:爱写Bug

LeetCode 599: 两个列表的最小索引总和 Minimum Index Sum of Two Lists的更多相关文章

  1. [Swift]LeetCode599. 两个列表的最小索引总和 | 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 ...

  2. Java实现 LeetCode 599 两个列表的最小索引总和(使用hash提高效率)

    599. 两个列表的最小索引总和 假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. 你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅 ...

  3. Leetcode 599.两个列表的最小索引总和

    两个列表的最小索引总和 假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. 你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅. 如果答 ...

  4. 领扣(LeetCode)两个列表的最小索引总和 个人题解

    假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. 你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅. 如果答案不止一个,则输出所有答 ...

  5. 【leetcode 简单】 第一百五十题 两个列表的最小索引总和

    假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. 你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅. 如果答案不止一个,则输出所有答 ...

  6. 【Leetcode_easy】599. Minimum Index Sum of Two Lists

    problem 599. Minimum Index Sum of Two Lists 题意:给出两个字符串数组,找到坐标位置之和最小的相同的字符串. 计算两个的坐标之和,如果与最小坐标和sum相同, ...

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

  8. C#LeetCode刷题之#599-两个列表的最小索引总和​​​​​​​​​​​​​​(Minimum Index Sum of Two Lists)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3802 访问. 假设Andy和Doris想在晚餐时选择一家餐厅,并 ...

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

随机推荐

  1. Oracle 11g与12c的审计详解

    最近遇到一些脚本诱发的审计相关BUG,感觉有必要重新梳理一下11g与12c的审计模式,于是根据官网修正了一下以前的一篇笔记这里发出来. 一.审计功能的开启: SQL> show paramete ...

  2. zhy2_rehat6_mysql02 - 5.7主从搭建.txt

    1.0------------锁库: mysql>FLUSH TABLES WITH READ LOCK; 这个命令是全局读锁定,执行了命令之后所有库所有表都被锁定只读.一般都是用在数据库联机备 ...

  3. [PHP] RBAC权限与审批流的简单数据库构想

    权限部分:功能权限+数据权限 控制权限是界面按钮菜单的权限控制,数据权限是数据范围的控制 role(角色) ----------------- |id | ----------------- |nam ...

  4. 题解 P2286 【[HNOI2004]宠物收养场】

    这是题目链接 大家好,这个题我调了很久过了,所以想写题解 我用的平衡树是AVL树,平衡树界的老爷爷 这个树并不会很慢,主要是我初学,常数巨大 而且题目的 $ n = 80000$,可以接受 \(sol ...

  5. BZOJ3894/LG4313 文理分科 新建点最小割

    问题描述 BZOJ3894 LG4313 题解 显然一个人只能选文/理 -> 一个人只能属于文(S).理(T)集合中的一个 可以把选择文得到 \(art\) 的收益看做选择文失去 \(scien ...

  6. [算法模板]FFT-快速傅里叶变换

    [算法模板]FFT-快速傅里叶变换 感谢ZYW聚聚为我们讲解FFT~ 思路 我懒,思路和证明部分直接贴链接: rvalue LSJ-FFT与NTT基础 代码 主要思想是利用了单位根特殊的性质(n次单位 ...

  7. CMKAE简单实用指南

    CMake is an open-source, cross-platform family of tools designed to build, test and package software ...

  8. Spring Security从后台数据库查询实现登陆控制

    Spring Security框架是一个控制登陆的框架,通过配置文件获取后台的用户名及密码,进行比较进行登陆判断 使用步骤 1.导入依赖 <!-- 身份验证 --> <depende ...

  9. js 生成32位随机数,可用于微信支付流水号(前端生成)

    $(function () { /*生成32位随机流水号*/ /*默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1*/ var $chars = 'ABCDEFGHJKMNPQRSTWXYZ ...

  10. c++.net学习笔记

    Notes for c++ learning 程序根据什么特征来区分调用哪个重载函数? 只能靠参数而不能靠返回值类型的不同来区分重载函数. 编译器根据参数为每个重载函数产生不同的内部标识符 在Visu ...