Skip to content

Instantly share code, notes, and snippets.

@tratitude
Created May 13, 2018 08:42
Show Gist options
  • Save tratitude/a4b5c574f9a284d0f9a811ad00a72789 to your computer and use it in GitHub Desktop.
Save tratitude/a4b5c574f9a284d0f9a811ad00a72789 to your computer and use it in GitHub Desktop.
Approach#3 hash table
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
map<int, int> hashTable;
vector<int> outVec;
int i;
for(i=0; i<nums.size(); i++){
map<int, int>::iterator it = hashTable.find(target-nums[i]);
if(it != hashTable.end()){
outVec.push_back(it->second);
outVec.push_back(i);
return outVec;
}
hashTable.insert(pair<int, int>(nums[i],i));
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment