diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..5dc233d --- /dev/null +++ b/main.cpp @@ -0,0 +1,17 @@ +#include +#include + +class Solution { +public: + std::vector twoSum(std::vector& nums, int target) { + std::unordered_map num_map; // Stores number and its index + for (int i = 0; i < nums.size(); ++i) { + int complement = target - nums[i]; + if (num_map.find(complement) != num_map.end()) { + return {num_map[complement], i}; // Found the two indices + } + num_map[nums[i]] = i; // Store index of the current number + } + return {}; + } +}; \ No newline at end of file