leetcode/src/s0001_two_sum.cpp

14 lines
332 B
C++

#include "s0001_two_sum.hpp"
vector<int> S0001::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 {};
}