Skip to content

Instantly share code, notes, and snippets.

@ccincotti3
Created September 6, 2022 20:52
Show Gist options
  • Save ccincotti3/6fdac6d6a2885f13a076acf89cb88593 to your computer and use it in GitHub Desktop.
Save ccincotti3/6fdac6d6a2885f13a076acf89cb88593 to your computer and use it in GitHub Desktop.
twoSum Leetcode C++ solution - accompanying solution at www.carmencincotti.com
// View accompanying article at www.carmencincotti.com/algorithms/leetcode/01-twosum
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> m;
for(int i; i<nums.size();i++) {
int complement = target-nums[i];
if(m.find(complement) != m.end())
return {i, m[complement]};
else
m[nums[i]] = i;
}
return {-1, -1}; // Needed to compile.
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment