-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path954.cpp
31 lines (30 loc) · 860 Bytes
/
954.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
class Solution {
public:
bool canReorderDoubled(vector<int> &A) {
auto size = A.size();
if (size < 2)
return true;
for (int i = 0; i < A.size(); ++i)
if (A[i] < 0)
A[i] = -A[i];
sort(A.begin(), A.end());
for (int i = 0; i < size; ++i) {
bool deleted = false;
for (int j = i + 1; j < size; ++j) {
if (A[j] == A[i] * 2) {
A.erase(A.begin() + i);
A.erase(A.begin() + j - 1);
size -= 2;
deleted = true;
break;
} else if (A[j] > A[i] * 2)
return false;
}
if (deleted)
i = -1;
}
if (A.empty())
return true;
return false;
}
};