-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path869.cpp
More file actions
44 lines (42 loc) · 1.15 KB
/
Copy path869.cpp
File metadata and controls
44 lines (42 loc) · 1.15 KB
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
class Solution {
public:
bool power = false;
bool reorderedPowerOf2(int N) {
vector<int> n, cur;
while (N) {
n.push_back(N % 10);
N /= 10;
}
vector<bool> visited(n.size(), false);
recurse(cur, n, visited, 0, n.size());
return power;
}
void recurse(vector<int> &cur, vector<int> &n, vector<bool> &visited, int i, int m) {
if (i == m) {
int temp = 0;
for (int k = 0; k < cur.size(); ++k)
temp = temp * 10 + cur[k];
while (temp > 1) {
int t = temp % 2;
if (t)
return;
temp /= 2;
}
power = true;
return;
}
for (int j = 0; j < m; ++j) {
if(i == 0 && n[j] == 0)
continue;
if (!visited[j]) {
visited[j] = true;
cur.push_back(n[j]);
recurse(cur, n, visited, i + 1, m);
if (power)
return;
visited[j] = false;
cur.pop_back();
}
}
}
};