-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathtwo-sum.cc
36 lines (36 loc) · 898 Bytes
/
two-sum.cc
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
// Two Sum
class Solution {
public:
vector<int> twoSum(vector<int> &a, int s) {
vector<int> r(a.size());
iota(r.begin(), r.end(), 0);
sort(r.begin(), r.end(), [&](int x, int y) { return a[x] < a[y]; });
for (size_t i = 0, j = a.size()-1; i < j; i++) {
while (j > i+1 && a[r[i]]+a[r[j]] > s) j--;
if (a[r[i]]+a[r[j]] == s) {
int x = r[i], y = r[j];
r.clear();
if (x > y) swap(x, y);
r.push_back(x);
r.push_back(y);
break;
}
}
return r;
}
};
///
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> m;
int n = nums.size();
for (int i = 0; i < n; i++) {
auto it = m.find(target - nums[i]);
if (it != m.end())
return {it->second, i};
m.insert({nums[i], i});
}
return {};
}
};