代码拉取完成,页面将自动刷新
// Time: O((m + n) * l), m is the size of list1, n is the size of list2
// Space: O(m * l), l is the average string length
class Solution {
public:
vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
unordered_map<string, int> lookup;
for (int i = 0; i < list1.size(); ++i) {
lookup[list1[i]] = i;
}
vector<string> result;
int min_sum = numeric_limits<int>::max();
for (int j = 0; j < list2.size() && j <= min_sum; ++j) {
if (lookup.count(list2[j])) {
auto sum = j + lookup[list2[j]];
if (sum < min_sum) {
result.clear();
result.emplace_back(list2[j]);
min_sum = sum;
} else if (sum == min_sum)
result.emplace_back(list2[j]);
}
}
return result;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。