mirror of
https://github.com/arkorty/LeetCode.git
synced 2026-03-17 16:51:46 +00:00
Initial commit
This commit is contained in:
18
Easy/two-sum/solution.cpp
Normal file
18
Easy/two-sum/solution.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
class Solution {
|
||||
public:
|
||||
std::vector<int> twoSum(std::vector<int> &nums, int target) {
|
||||
std::unordered_map<int, int> hmap;
|
||||
|
||||
for (int i = 0; i < nums.size(); ++i) {
|
||||
int diff = target - nums[i];
|
||||
|
||||
if (hmap.find(diff) != hmap.end()) {
|
||||
return {hmap[diff], i};
|
||||
} else {
|
||||
hmap[nums[i]] = i;
|
||||
}
|
||||
}
|
||||
|
||||
return {-1, -1};
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user