给定正整数 N
,我们按任何顺序(包括原始顺序)将数字重新排序,注意其前导数字不能为零。
如果我们可以通过上述方式得到 2 的幂,返回 true
;否则,返回 false
。
输入: 1 输出: true
输入: 10 输出: false
输入: 16 输出: true
输入: 24 输出: false
输入: 46 输出: true
1 <= N <= 10^9
impl Solution {
pub fn reordered_power_of2(n: i32) -> bool {
let mut power_of2 = Vec::new();
for i in 0..30 {
let mut arr = vec![0; 10];
let mut x = 2_usize.pow(i);
while x > 0 {
arr[x % 10] += 1;
x /= 10;
}
power_of2.push(arr);
}
let mut arr = vec![0; 10];
let mut n = n as usize;
while n > 0 {
arr[n % 10] += 1;
n /= 10;
}
power_of2.contains(&arr)
}
}