This commit is contained in:
2022-03-08 15:54:17 +08:00
parent 57d75f7596
commit 2af9a81f28
5 changed files with 131 additions and 8 deletions

14
src/s1_two_sum.cpp Normal file
View File

@@ -0,0 +1,14 @@
#include "s1_two_sum.hpp"
using namespace std;
vector<int> Solution::twoSum(vector<int>& nums, int target) {
unordered_map<int, int> hashtable;
for (int i = 0; i < nums.size(); ++i) {
auto it = hashtable.find(target - nums[i]);
if (it != hashtable.end()) {
return {it->second, i};
}
hashtable[nums[i]] = i;
}
return {};
}