[leetcode]599. Minimum Index Sum of Two Lists

Posted stAr_1

tags:

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

哈希表应用比较典型

public String[] findRestaurant(String[] list1, String[] list2) {
        //哈希表存字符串和对应下标
        Map<String,Integer> map = new HashMap<>();
        List<String> res = new ArrayList<>();
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < list1.length; i++) {
            map.put(list1[i],i);
        }
        //不断更新结果
        for (int i = 0; i < list2.length; i++) {
            String str = list2[i];
            if (map.containsKey(str))
            {
                if (i+map.get(str)<min)
                {
                    res.clear();
                    res.add(str);
                    min = i+map.get(str);
                }
                else if (i+map.get(str)==min) res.add(str);
            }
        }
        String[] s = new String[res.size()];
        for (int i = 0; i < res.size(); i++)
        {
            s[i] = res.get(i);
        }
        return s;
    }

 

以上是关于[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里找到相同的并且位置总和最靠前的)