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

执行用时:268 ms, 在所有 Kotlin 提交中击败了100.00% 的用户
内存消耗:35.8 MB, 在所有 Kotlin 提交中击败了100.00% 的用户
通过测试用例:136 / 136

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
fun findRestaurant(list1: Array<String>, list2: Array<String>): Array<String> {
val map1: HashMap<String, Int> = hashMapOf()
val res = mutableListOf<String>()
var minIndex = 10001;
for (i in list1.indices) {
map1[list1[i]] = i
}
for (i in list2.indices) {
if (map1[list2[i]] != null && i + map1[list2[i]]!! <= minIndex) {
minIndex = i + map1[list2[i]]!!
}
}
for (i in list2.indices) {
if (map1[list2[i]] != null && i + map1[list2[i]]!! <= minIndex) {
res.add(list2[i])
}
}
return res.toTypedArray()
}
}

599. 两个列表的最小索引总和
https://pisces34.github.io/2022/03/15/leetcode/599/
发布于
2022年3月15日
许可协议