leetcode/src/s0001_two_sum.cpp

14 lines
332 B
C++
Raw Normal View History

2022-03-12 03:04:49 +00:00
#include "s0001_two_sum.hpp"
2022-03-08 07:54:17 +00:00
2022-11-30 10:20:36 +00:00
vector<int> S0001::twoSum(vector<int>& nums, int target) {
2022-03-08 07:54:17 +00:00
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 {};
}