题目
https://leetcode.com/problems/reordered-power-of-2/
题解
class Solution {
public boolean reorderedPowerOf2(int n) {
Set<Long> set = new HashSet<>();
int target = 1;
for (int i = 0; i < 31; i++) {
set.add(compress(target));
target <<= 1;
}
return set.contains(compress(n));
}
public long compress(int n) {
int[] count = new int[10];
for (char c : String.valueOf(n).toCharArray()) {
count[c - '0']++;
}
long res = 0;
for (int c : count) {
res *= 10;
res += c;
}
return res;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)