首页 > 代码库 > [LeetCode] 599. Minimum Index Sum of Two Lists

[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 = http://www.mamicode.com/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