-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path869.cpp
More file actions
50 lines (47 loc) · 916 Bytes
/
Copy path869.cpp
File metadata and controls
50 lines (47 loc) · 916 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "common.h"
class Solution {
public:
bool reorderedPowerOf2(int n) {
vector<int> a;
int t = n;
while (t) {
a.push_back(t % 10);
t /= 10;
}
std::sort(a.begin(), a.end());
n = 0;
for (int i = a.size() - 1; i >= 0; --i) {
n = n * 10 + a[i];
}
int m = 1;
while (m <= n) {
vector<int> b;
int k = m;
while (k) {
b.push_back(k % 10);
k /= 10;
}
std::sort(b.begin(), b.end());
if (IsVectorEqual(a, b)) {
return true;
}
m <<= 1;
}
return false;
}
bool IsVectorEqual(const vector<int>& a, const vector<int>& b) {
if (a.size() != b.size()) {
return false;
}
for (auto i = 0; i < a.size(); ++i) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}
};
int main() {
Solution s;
cout << s.reorderedPowerOf2(46) << endl;
}