leetcode two sum problem solution with c++
#include <vector>
#include <unordered_map>
#include <iostream>
std::vector<int> twoSum(std::vector<int>& nums, int target) {
std::unordered_map<int, int> numMap; // Map to store number and its index
for (int i = 0; i < nums.size(); ++i) {
int complement = target - nums[i];
if (numMap.find(complement) != numMap.end()) {
return {numMap[complement], i};
}
numMap[nums[i]] = i;
}
return {}; // Return an empty vector if no solution found, though per problem statement there is always one solution
}
int main() {
// Example 1
std::vector<int> nums1 = {2, 7, 11, 15};
int target1 = 9;
std::vector<int> result1 = twoSum(nums1, target1);
std::cout << "[" << result1[0] << "," << result1[1] << "]" << std::endl;
// Example 2
std::vector<int> nums2 = {3, 2, 4};
int target2 = 6;
std::vector<int> result2 = twoSum(nums2, target2);
std::cout << "[" << result2[0] << "," << result2[1] << "]" << std::endl;
// Example 3
std::vector<int> nums3 = {3, 3};
int target3 = 6;
std::vector<int> result3 = twoSum(nums3, target3);
std::cout << "[" << result3[0] << "," << result3[1] << "]" << std::endl;
return 0;
}
No comments