-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path599.cpp
26 lines (26 loc) · 804 Bytes
/
599.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public:
vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
unordered_map<string, int> s;
vector<string> res;
int minv = list1.size() + list2.size();
for(int i = 0; i < list1.size(); i++)
s.insert(make_pair(list1[i], i));
for(int i = 0; i < list2.size(); i++)
{
auto tem = s.find(list2[i]);
if(tem != s.end())
{
if(i + tem->second < minv)
{
res.clear();
res.push_back(tem->first);
minv = i+tem->second;
}
else if(i + tem->second == minv)
res.push_back(tem->first);
}
}
return res;
}
};