-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.2.cpp
More file actions
executable file
·53 lines (46 loc) · 1.22 KB
/
1.2.cpp
File metadata and controls
executable file
·53 lines (46 loc) · 1.22 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
52
53
#include <iostream>
#include <vector>
#include <unordered_map>
#include <ctime>
using namespace std;
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target)
{
// unordered_map<int, int> hash;
vector <int> results;
for (int i = 0; i < nums.size(); i++)
{
int find = target - nums[i];
for (int j = i; j < nums.size(); j++)
{
if (nums[j] == find)
{
results.push_back(j+1);
results.push_back(i+1);
return results;
}
}
}
cout << "Can't find any." << endl;
return results;
}
};
int main()
{
vector<int> v = {2, 7, 11, 15};
clock_t startTime = clock();
Solution S;
vector<int> res;
res = S.twoSum(v,9);
if (res.size() > 0)
{
cout << "First index: " << res.back() << ": " << v[res.back() - 1] << endl;
cout << "Second index: " << res.front() << ": " << v[res.front() - 1] << endl;
cout << "Result = " << v[res.back() - 1] + v[res.front() - 1] << endl;
cout << "Size = " << res.size() << endl;
res.clear();
}
double elapsedSeconds = (double)(clock() - startTime) / CLOCKS_PER_SEC;
cout << "RUN TIME: " << elapsedSeconds << endl;
}