-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_TwoSum.cpp
More file actions
36 lines (31 loc) · 737 Bytes
/
01_TwoSum.cpp
File metadata and controls
36 lines (31 loc) · 737 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
#include <iostream>
#include <string.h>
#include <map>
#include <vector>
#include <hash_map>
//使用map表 算法复杂度:nlog(n)
//从列表中
using namespace std;
std::vector<int, int> twoSum(int *pnArray, int cnArray, int target)
{
map<int,int> mapArray;
for (size_t i = 0; i < cnArray; i++)
{
/* code */
int num = target - pnArray[i];
std::map<int,int>::iterator it = mapArray.find(target);
if(it != mapArray.end())
{
return {it->second,pnArray[i]};
}
mapArray[pnArray[i]] = num;
}
}
int main()
{
int nArray[] ={2,7,11,15,19};
vector<int, int> list;
list = twoSum(nArray,5,9);
cout << "Test end"<< endl;
return 0;
}