Two-sum solution

This commit is contained in:
2025-12-06 10:00:23 +00:00
parent de106b2e33
commit 6363aeaed3
4 changed files with 57 additions and 0 deletions
+16
View File
@@ -0,0 +1,16 @@
class Solution{
public:
vector<int> twoSum(vector<int> &nums, int target) {
unordered_map<int, int> hash;
for (int i = 0; i < nums.size(); i++) {
hash[nums[i]] = i;
}
for (int i = 0; i < nums.size(); i++) {
int comp = target - nums[i];
if (hash.find(comp) != hash.end() && hash[comp] != i) {
return {i, hash[comp]};
}
}
return {};
}
};