美文网首页
2022-03-13 599. 两个列表的最小索引总和

2022-03-13 599. 两个列表的最小索引总和

作者: 16孙一凡通工 | 来源:发表于2022-03-15 08:49 被阅读0次

599. 两个列表的最小索引总和

java版本:

class Solution {
    public String[] findRestaurant(String[] list1, String[] list2) {
        // 利用排序
        // Arrays.sort(list1);
        // Arrays.sort(list2);
        int n=list1.length+list2.length;
        HashMap<String,Integer> hashmap=new HashMap<>();
        for(int i=0;i<list1.length;i++){
        
            hashmap.put(list1[i],i);
        }
        int minIndex=n,index=0,temp=0,count=0;
        List<String> arr=new ArrayList<>();
       
          for(int i=0;i<list2.length;i++){
         if (hashmap.containsKey(list2[i])) {
                temp= i+hashmap.get(list2[i]);
          
          
                if (temp<minIndex ){
                    arr.clear();
                    arr.add(list2[i]);
                    minIndex = temp;
                } else if (temp==minIndex) {
                    arr.add(list2[i]);
                }

           
           }
        }
        String[] res=arr.toArray(new String[arr.size()]);
        //  for(int i=0;i<count;i++){
        //   res[i]=list2[arr.get(i)];
        //  }
       
        return res;
  
    }
}

相关文章

网友评论

      本文标题:2022-03-13 599. 两个列表的最小索引总和

      本文链接:https://www.haomeiwen.com/subject/ylujdrtx.html