文章作者:Tyan
博客:noahsnail.com | CSDN |
1. Description
2. Solution
class Solution {
public:
vector findRestaurant(vector& list1, vector& list2) {
int min = INT_MAX;
vector result;
unordered_map m;
for(int i = 0; i < list1.size(); i++) {
m[list1[i]] = i;
}
for(int i = 0; i < list2.size(); i++) {
if(m.find(list2[i]) != m.end()) {
int index = m[list2[i]] + i;
if(index < min) {
min = m[list2[i]] + i;
result.clear();
result.emplace_back(list2[i]);
}
else if(index == min) {
result.emplace_back(list2[i]);
}
}
}
return result;
}
};
Reference
- https://leetcode.com/problems/minimum-index-sum-of-two-lists/description/