-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwoSum.cpp
More file actions
57 lines (48 loc) · 1003 Bytes
/
twoSum.cpp
File metadata and controls
57 lines (48 loc) · 1003 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <vector>
#include <iostream>
using std::vector;
using namespace std;
class Solution
{
public:
vector<int> twoSum(vector<int> &nums, int target)
{
int i = 0, j = 0, foundPair = false;
int len = nums.size();
vector<int> result;
for (i = 0; i < len; i++)
{
for (j = 0; j < len; j++)
{
if (i != j)
{
int sum = nums[i] + nums[j];
cout << "sum" << sum;
if (sum == target)
{
result.push_back(i);
result.push_back(j);
foundPair = true;
break;
}
}
}
if (foundPair)
break;
}
return result;
}
};
int main()
{
Solution sol;
int nums[4] = {2, 7, 11, 15};
vector<int> g1;
int len = sizeof(nums) / sizeof(nums[0]);
cout << "length: " << len << endl;
for (int i = 0; i < len; i++)
g1.push_back(nums[i]);
for (auto i = g1.begin(); i != g1.end(); ++i)
cout << *i << " ";
sol.twoSum(g1, 9);
}