Skip to content

Instantly share code, notes, and snippets.

@pinglunliao
Last active November 5, 2019 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pinglunliao/4b0e75dfde7d39468449a8f569dbfdbb to your computer and use it in GitHub Desktop.
Save pinglunliao/4b0e75dfde7d39468449a8f569dbfdbb to your computer and use it in GitHub Desktop.
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
multimap<int, int> nTable;
multimap<int, int>::iterator it, cur;
vector<int> result;
for(int i = 0; i < nums.size(); i++)
nTable.insert(make_pair(nums[i], i));
for(cur = nTable.begin(); cur != nTable.end(); cur++) {
int c = target - cur->first;
it = nTable.find(c);
if(it != nTable.end() && cur->second != it->second) {
if(it->first == cur->first) {
result.push_back(it->second);
result.push_back(cur->second);
return result;
}
result.push_back(cur->second);
result.push_back(it->second);
return result;
}
}
return result;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment