Created
May 13, 2018 08:42
-
-
Save tratitude/a4b5c574f9a284d0f9a811ad00a72789 to your computer and use it in GitHub Desktop.
Approach#3 hash table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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