-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1a.cpp
33 lines (32 loc) · 1006 Bytes
/
1a.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
class Solution {
public:
typedef pair<int, int> PII;
unordered_map<int, int> h;
int bs(vector<PII>& nums, int l, int r, int x) {
int mid = 0;
while (l <= r) {
mid = (l + r) >> 1;
if (nums[mid].first == x) return mid;
if (nums[mid].first > x) r = mid - 1;
else l = mid + 1;
}
return -1;
}
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> ret;
vector<PII> arr;
for (int i = 0; i < nums.size(); i++) {
arr.push_back(PII(nums[i], i));
}
sort(arr.begin(), arr.end());
for (int i = 0; i < arr.size(); i++) {
int ind = bs(arr, i + 1, arr.size() - 1, target - arr[i].first);
if (ind == -1) continue;
int ind1 = arr[i].second, ind2 = arr[ind].second;
if (ind1 > ind2) swap(ind1, ind2);
ret.push_back(ind1);
ret.push_back(ind2);
}
return ret;
}
};