#include #include #include using namespace std; class Solution { public: vector twoSum(vector &nums, int target) { std::unordered_map hset; vector r; for (int i = 0; i < nums.size(); ++i) { int c = target - nums[i]; auto iter = hset.find(c); if (iter != hset.end() && iter->second != i) { r.push_back(i); r.push_back(iter->second); return r; } hset.insert(std::make_pair(nums[i], i)); } return r; } }; int main() { Solution test; int arr[] = {3, 2, 4}; vector ret; vector vec(arr, arr + 4); ret = test.twoSum(vec, 6); for (auto i = ret.begin(); i != ret.end(); i++) { cout << *i << ' '; } return 0; }