You are given an integer n. We reorder the digits in any order (including the original order) such that the leading digit is not zero.
Return true if and only if we can do this so that the resulting number is a power of two.
Example 1:
Input: n = 1 Output: true
Example 2:
Input: n = 10 Output: false
Constraints:
1 <= n <= 109class Solution {
public:
string sorter(string s){
sort(s.begin(), s.end());
return s;
}
bool reorderedPowerOf2(int n) {
int x = 1;
string s = sorter(to_string(n));
for (int i=0; i<30; i++){
if(s==sorter(to_string(x)))
return true;
x <<= 1;
}
return false;
}
};class Solution {
boolean valid = false;
public boolean reorderedPowerOf2(int n) {
int len = 0;
int[] freq = new int[10];
while (n > 0) {
freq[n % 10]++;
n /= 10;
len++;
}
for (int i = 1; i < 10; i++) {
if (freq[i] == 0)
continue;
freq[i]--;
dfs(freq, i, len - 1);
freq[i]++;
}
return valid;
}
private void dfs(int[] freq, int curr, int len) {
if (valid)
return;
if (len == 0) {
// System.out.println(curr);
valid |= isPowerOf2(curr);
return;
}
for (int i = 0; i < 10; i++) {
if (freq[i] == 0)
continue;
freq[i]--;
dfs(freq, 10 * curr + i, len - 1);
freq[i]++;
}
}
private boolean isPowerOf2(int x) {
while (x > 0) {
if (x % 2 == 1)
return x == 1;
x = x >> 1;
}
return false;
}
}