Created
June 22, 2020 20:02
-
-
Save JyotinderSingh/468056e7beb2e1ea6f42107fee0f1ae4 to your computer and use it in GitHub Desktop.
Two Sum (LeetCode) | Linear Time Algorithm
This file contains hidden or 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) { | |
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