Skip to content

Instantly share code, notes, and snippets.

@JyotinderSingh
Created June 22, 2020 20:02
Show Gist options
  • Save JyotinderSingh/468056e7beb2e1ea6f42107fee0f1ae4 to your computer and use it in GitHub Desktop.
Save JyotinderSingh/468056e7beb2e1ea6f42107fee0f1ae4 to your computer and use it in GitHub Desktop.
Two Sum (LeetCode) | Linear Time Algorithm
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> mp;
for(int i = 0; i < nums.size(); ++i) {
int complement = target - nums[i];
if(mp.find(complement) != mp.end()) {
return {mp[complement], i};
}
mp[nums[i]] = i;
}
return {-1, -1};
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment