1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class Solution { public: map<int, int> idxMap; vector<int> twoSum(vector<int>& nums, int target) { for (int i = 0; i < nums.size(); i++) idxMap[nums[i]] = i; for (int i = 0; i < nums.size(); i++) { int remain = target - nums[i]; if (idxMap.find(remain) != idxMap.end() && idxMap[remain] != i) { return {i, idxMap[remain]}; } } return {}; } }; |