mirror of
https://github.com/Manoj-HV30/dsa-competitive-programming.git
synced 2026-05-16 19:35:22 +00:00
17 lines
456 B
C++
17 lines
456 B
C++
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 {};
|
|
}
|
|
};
|