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拍平成array
// index 0 1 2 3 4 5 6 7 8 9
// num=2566 -> [0,0,1,0,0,1,2,0,0,0]
int[] count = new int[10];
for (char c : String.valueOf(n).toCharArray()) {
count[c - '0']++;
}
// 对array状态压缩
// [0,0,1,0,0,1,2,0,0,0] -> 0010012000
long res = 0;
for (int c : count) {
res *= 10;
res += c;
}
return res;
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://hanquan.blog.csdn.net/article/details/125609631
内容来源于网络,如有侵权,请联系作者删除!