869. Reordered Power of 2

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 <= 109
class Solution {
    public boolean reorderedPowerOf2(int n) {
        int[] nn = count(n);
        for(int i = 0; i < 32; i++) {
            if(Arrays.equals(nn, count(1 << i))) return true;
        }
        return false;
    }
    public int[] count(int n) {
        int[] arr = new int[10];
        while(n > 0) {
            arr[n % 10]++;
            n /= 10;
        }
        return arr;
    }
}

和power,2有关的要想到1移位

本题本质是比较两个数0-9每一位的个数是否相同

posted @ 2022-08-26 10:21  Schwifty  阅读(16)  评论(0编辑  收藏  举报