码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] 599. Minimum Index Sum of Two Lists

时间:2017-06-06 01:06:20      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:ring   least   ret   https   lists   ini   public   value   targe   

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

标签:ring   least   ret   https   lists   ini   public   value   targe   

原文地址:http://www.cnblogs.com/chencode/p/minimum-index-sum-of-two-lists.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!