[LeetCode] 599. Minimum Index Sum of Two Lists

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode] 599. Minimum Index Sum of Two Lists相关的知识,希望对你有一定的参考价值。

https://leetcode.com/problems/minimum-index-sum-of-two-lists/

public class Solution {
    public String[] findRestaurant(String[] list1, String[] list2) {
        int leastSum = Integer.MAX_VALUE;
        List<String> result = new ArrayList<>();
        Map<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 value = map.get(list2[i]) + i;
                if (leastSum > value) {
                    leastSum = value;
                    result = new ArrayList<>();
                }
                if (leastSum == value) {
                    result.add(list2[i]);
                }
            }
        }
        return result.toArray(new String[result.size()]);
    }
}

 

以上是关于[LeetCode] 599. Minimum Index Sum of Two Lists的主要内容,如果未能解决你的问题,请参考以下文章

[leetcode-599-Minimum Index Sum of Two Lists]

LeetCode 599. Minimum Index Sum of Two Lists

[LeetCode] 599. Minimum Index Sum of Two Lists

leetcode-599-Minimum Index Sum of Two Lists

[leetcode]599. Minimum Index Sum of Two Lists

LeetCode 599. Minimum Index Sum of Two Lists (从两个lists里找到相同的并且位置总和最靠前的)