public class Solution {
public String[] findRestaurant(String[] list1, String[] list2) {
int i = 0;
i = 0;
Map<String, Integer> map2 = new HashMap<String, Integer>();
for (String ele : list2 ) {
map2.put(ele, i++);
}
int min = Integer.MAX_VALUE;
i = 0;
for (String ele : list1) {
if (map2.containsKey(ele)) {
if (map2.get(ele) + i < min ) min = map2.get(ele) + i;
}
i++;
}
i = 0;
LinkedList<String> res = new LinkedList<String>();
for (String ele : list1) {
if (map2.containsKey(ele)) {
if (map2.get(ele) + i == min ) res.add(ele);
}
i++;
}
return res.toArray(new String[res.size()]);
}
}
[leetCode刷题笔记]599. Minimum Index Sum of Two Lists
最新推荐文章于 2022-02-18 14:14:35 发布