add: 220314 [cpp]

This commit is contained in:
Lam Haoyin 2022-03-14 18:49:02 +08:00
parent 9908a3dc24
commit 74049b8dff
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
2 changed files with 32 additions and 1 deletions

31
cpp/2203/220314-CN.cpp Normal file
View File

@ -0,0 +1,31 @@
#include <vector>
#include <string>
#include <unordered_map>
class Solution {
public:
static std::vector<std::string> findRestaurant(const std::vector<std::string>& list1, const std::vector<std::string>& list2) {
std::unordered_map<std::string, int> map;
int m = list1.size(), n = list2.size();
for (int i = 0; i < m; ++i) {
map[list1[i]] = i;
}
int retMin = 0x7FFFFFFF;
std::vector<std::string> ret;
for (int i = 0; i < n; ++i) {
if (map.count(list2[i])) {
int x = i + map[list2[i]];
if (x < retMin) {
ret = {list2[i]};
retMin = x;
} else if (x == retMin) {
ret.push_back(list2[i]);
}
}
}
return ret;
}
};

View File

@ -3,4 +3,4 @@ PROJECT(2203)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2203 220313-CN.cpp) ADD_EXECUTABLE(2203 220314-CN.cpp)