class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int, int> map; vector<int> ans; for(int i = 0; i < nums.size(); i++) { map[target-nums[i]] = i+1; } for(int i = 0; i < nums.size(); i++) { if(map[nums[i]] && map[nums[i]]-1 != i) { ans.push_back(map[target-nums[i]]-1); ans.push_back(i); } } ans.erase(unique(ans.begin(), ans.end()), ans.end()); return ans; } };
class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int, int> map; vector<int> ans; for(int i = 0; i < nums.size(); i++) { if(map.find(target - nums[i]) != map.end()) { ans.push_back(map[target-nums[i]]); ans.push_back(i); } else { map[nums[i]] = i; } } return ans; } };