Java easy understand method


  • 0
    N

    public class Solution {
    public String[] findRestaurant(String[] list1, String[] list2) {
    List<String> temp = Arrays.asList(list1);
    int min = Integer.MAX_VALUE;
    int index = 0;
    ArrayList<String> array = new ArrayList<String>();
    for(int i = 0; i < list2.length; i++)
    {
    if(temp.contains(list2[i]))
    {
    index = i + temp.indexOf(list2[i]);
    if(index <= min)
    {
    if(index == min)
    {
    min = index;
    array.add(list2[i]);
    }
    else
    {
    min = index;
    array.clear();
    array.add(list2[i]);
    }
    }
    }
    }
    return array.toArray(new String[0]);
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.