forked from moranzcw/LeetCode-NOTES
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.cpp
More file actions
28 lines (27 loc) · 782 Bytes
/
solution.cpp
File metadata and controls
28 lines (27 loc) · 782 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
class Solution {
public:
bool canReorderDoubled(vector<int>& A) {
sort(A.begin(),A.end());
unordered_map<int,int> used;
for(auto &i:A){
if(used.find(i) == used.end())
used.insert(pair<int,int>(i,1));
else
used[i] ++;
}
for(int i=0;i<A.size();i++){
if((used.find(A[i]) != used.end() ) && (used[A[i]] != 0 ) ){
used[A[i]] --;
int d = A[i] * 2;
if(d < 0)
d = A[i] / 2;
if((used.find(d) != used.end()) && (used[d] != 0)){
used[d] --;
}
else
return false;
}
}
return true;
}
};