Skip to content

Commit 0b4acbb

Browse files
committed
add first exercise with cpp
1 parent 0397186 commit 0b4acbb

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

cpp/0001_two_sum/0001_two_sum

106 KB
Binary file not shown.

cpp/0001_two_sum/0001_two_sum.cc

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,42 @@
11
#include <iostream>
2+
#include <unordered_map>
3+
#include <vector>
4+
25
using std::cout;
6+
using std::unordered_map;
7+
using std::vector;
8+
9+
class Solution
10+
{
11+
public:
12+
vector<int> twoSum(vector<int> &nums, int target)
13+
{
14+
unordered_map<int, int> umap;
15+
vector<int> result = {};
16+
for (int i = 0; i < nums.size(); i++)
17+
{
18+
const int current = nums[i];
19+
20+
if (umap.count(current) > 0)
21+
{
22+
result = {i, umap[current]};
23+
break;
24+
}
25+
26+
const int diff = target - nums[i];
27+
umap[diff] = i;
28+
}
29+
30+
return result;
31+
}
32+
};
333

434
int main()
535
{
6-
cout << "hola";
7-
}
36+
vector<int> nums = {2, 7, 11, 15};
37+
auto result = Solution().twoSum(nums, 9);
38+
for (auto n : result)
39+
{
40+
cout << n << " ";
41+
}
42+
}

0 commit comments

Comments
 (0)