-
Notifications
You must be signed in to change notification settings - Fork 287
/
AC_map_hash_nlogn.cpp
52 lines (45 loc) · 1.2 KB
/
AC_map_hash_nlogn.cpp
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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: ac_hash_map_nlogn.cpp
* Create Date: 2014-11-26 11:09:16
* Descripton: Save with map.
* time:nlogn, memory:nlogn
*/
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
class Solution {
private:
map<int, int> idOfNum;
public:
vector<int> twoSum(vector<int> &numbers, int target) {
vector<int> index;
idOfNum.clear();
int sz = numbers.size();
for (int i = 0; i < sz; i++) {
int rest = target - numbers[i];
if (idOfNum.find(rest) != idOfNum.end()) {
index.push_back(idOfNum[rest]);
index.push_back(i + 1);
return index;
}
idOfNum[numbers[i]] = i + 1;
}
// Program never go here, because
// "each input would have exactly one solution"
}
};
int main() {
Solution s;
vector<int> in;
int n, t;
cin >> n;
while (n--) {
cin >> t;
in.push_back(t);
}
cin >> t;
in = s.twoSum(in, t);
cout << in[0] << ' ' << in[1] << endl;
return 0;
}