main.cpp Ekle
This commit is contained in:
parent
c9adcdc34d
commit
d36600c6bd
17
main.cpp
Normal file
17
main.cpp
Normal 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 {};
|
||||||
|
}
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user