leetcode/src/s1_two_sum.cpp

15 lines
353 B
C++

#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 {};
}