-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_two_sum.cpp
More file actions
51 lines (46 loc) · 1.31 KB
/
1_two_sum.cpp
File metadata and controls
51 lines (46 loc) · 1.31 KB
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
41
42
43
44
45
46
47
48
49
50
51
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <unordered_map>
#include <iomanip>
#include <chrono>
#include <unordered_set>
using namespace std;
template class unordered_map<int, int>;
class Solution
{
public:
vector<int> twoSum(vector<int> &nums, int target)
{
unordered_map<int, int> missingNumbers{};
for (int i = 0; i != nums.size(); ++i)
{
if (missingNumbers.count(target - nums[i]) != 0)
{
return {missingNumbers[target - nums[i]], i};
}
missingNumbers[nums[i]] = i;
}
return {0, 0};
}
};
int main()
{
auto start = std::chrono::high_resolution_clock::now();
Solution s;
std::vector<int> nums = {3, 3};
int target = 6;
std::vector<int> res = s.twoSum(nums, target);
// std::cout << res << std::endl;
copy(res.begin(), res.end(), std::ostream_iterator<int>(std::cout, "\n"));
// Timer
auto end = std::chrono::high_resolution_clock::now();
double time_taken =
std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
time_taken *= 1e-9;
std::cout << "Time taken by program is : " << std::fixed
<< time_taken << std::setprecision(9);
std::cout << " sec" << std::endl;
return 0;
}