The sum of the minimum index Leetcode brush 599. The two lists of the title java

Results of the:

by

Show details

When execution: 8 ms, defeated 100.00% of users in all Java submission

Memory consumption: 38.7 MB, defeated 97.85% of all users to submit in Java

topic:

Suppose Andy and Doris want to choose a restaurant for dinner, and they have expressed a favorite restaurant list, the name of each restaurant is represented by a string.

You need to help them with the least index and find their common favorite restaurant. If the answer is more than one output all the answers and do not consider the order. You can assume that there is always an answer.

Example 1:

Enter:
[ "Shogun", "Tapioca Express", "Burger King", "KFC"]
[ "Piatti", "at The Grill AT Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"]
Output: [ "Shogun" ]
explanation: their only common favorite restaurant is "Shogun".
Example 2:

Enter:
[ "Shogun", "Tapioca Express", "Burger King", "KFC"]
[ "KFC", "Shogun", "Burger King"]
Output: [ "Shogun"]
to explain: they co-favorite with minimal index and restaurant are "Shogun", which has the smallest index and 1 (0 + 1).
prompt:

Length two lists are [1, 1000] within.
Length of the string within the two lists will be in the range [1,30] of.
Index from 0 to 1 minus the length of the list.
Both lists are no duplicate elements.

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/minimum-index-sum-of-two-lists
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Ideas:

Create a hashmap to store values ​​and the corresponding index, why should it then, because it contains containsKey method, there is no judge may direct, then, is the minimum required output index, so you have to look at storage index calculation. Finally, traversing the second array, and if there is a smaller index is empty, re-join. If it is equal, then added directly to a.

Code:

class Solution {
    public String[] findRestaurant(String[] list1, String[] list2) {
        HashMap<String,Integer> map=new HashMap();
        for(int i=0;i<list1.length;i++)
        {
            map.put(list1[i],i);
        }
        List<String> result=new ArrayList<>();
        int sum;
        int min_sum=Integer.MAX_VALUE;
        for(int j=0;j<list2.length&&j<=min_sum;j++)
        {
            if(map.containsKey(list2[j]))
            {
                sum=j+map.get(list2[j]);
                if(sum<min_sum)
                {
                    result.clear();
                    result.add(list2[j]);
                    min_sum=sum;
                }else if(sum==min_sum)
                {
                    result.add(list2[j]);
                }
            }
        }
        //列表变字符串数组的重要方法
        return result.toArray(new String[result.size()]);
    }
}

 

Published 415 original articles · won praise 434 · views 210 000 +

Guess you like

Origin blog.csdn.net/qq_41901915/article/details/102504611