Two Sum
This commit is contained in:
14
src/s1_two_sum.cpp
Normal file
14
src/s1_two_sum.cpp
Normal 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 {};
|
||||
}
|
Reference in New Issue
Block a user