Created
September 6, 2022 20:52
-
-
Save ccincotti3/6fdac6d6a2885f13a076acf89cb88593 to your computer and use it in GitHub Desktop.
twoSum Leetcode C++ solution - accompanying solution at www.carmencincotti.com
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
// 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