leetcode/src/s0001_two_sum.cpp

15 lines
356 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
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 {};
}