main.cpp Ekle

This commit is contained in:
Yavuz Sava 2025-03-28 20:02:09 +00:00
parent c9adcdc34d
commit d36600c6bd

17
main.cpp Normal file
View File

@ -0,0 +1,17 @@
#include <vector>
#include <unordered_map>
class Solution {
public:
std::vector<int> twoSum(std::vector<int>& nums, int target) {
std::unordered_map<int, int> 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 {};
}
};