class Solution {
public:
vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
vector<string> res;
unordered_map<string,int> map;
int sum=list1.size()+list2.size();
for(int i=0;i<list1.size();i++)
map[list1[i]]=i;
for(int i=0;i<list2.size();i++){
if(map.find(list2[i])!=map.end()){
int temp=i+map[list2[i]];
if(temp<sum){
res.clear();
res.push_back(list2[i]);
sum=temp;
}
else if(temp==sum)
res.push_back(list2[i]);
}
}
return res;
}
};
599. Minimum Index Sum of Two Lists
最新推荐文章于 2022-06-18 12:05:26 发布