1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> hash;
for (int i = 0; i < nums.size(); i++) {
int complement = target - nums[i];
if (hash.find(complement) != hash.end()) {
return {hash[complement], i};
}
hash[nums[i]] = i;
}
return {};
}
};
Test Console
Case 1
Input
nums = [2,7,11,15], target = 9
Expected
[0,1]
Case 2
Input
nums = [3,2,4], target = 6
Expected
[1,2]
Case 3
Input
nums = [3,3], target = 6
Expected
[0,1]