-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwoSum.cpp
More file actions
40 lines (30 loc) · 695 Bytes
/
twoSum.cpp
File metadata and controls
40 lines (30 loc) · 695 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <vector>
#include <unordered_map>
// https://leetcode.com/problems/two-sum/
class Solution
{
public:
std::vector<int> twoSum(std::vector<int> &nums, int target)
{
std::unordered_map<int, int> numberIndexMap;
int numsSize = nums.size();
for (int i = 0; i < numsSize; i += 1)
{
int num = nums[i];
int rest = target - num;
int numberNotExist = numberIndexMap.count(num) == 0;
if (numberNotExist)
{
numberIndexMap[rest] = i;
}
else
{
std::vector<int> result{i, numberIndexMap[num]};
return result;
};
}
std::vector<int> result;
return result;
}
};