[Math_Medium] 869. Reordered Power of 2

原题:869. Reordered Power of 2

Starting with a positive 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 in a way such that the resulting number is a power of 2.1 <= N <= 10^9

题目大意:

给你一个数N(1 <= N <= 10^9),你可以对这个数中的每个数字进行重排组成另外一个数字,只要存在一个使得这个数是2的幂,则返回True;例如N=46,你可以重组为64,这样64就是2的幂了,则返回True,

解题思路:

因为1 <= N <= 10^9,而$ 2^{32}$>\(10^{9}\),因此,最多只有\(2^{0}\),\(2^{1}\),\(2^{2}\).....\(2^{31}\)共32个数,每个数都是不相同的,因此,我们只要判断N中每个数字的个数是否和之前的32个数中某一个数中的每一个数字的个数是否相同,只要相同数字的个数相同,那么就可以重新组合成那个数。因此,我们可以把N中的每个数字分解出来,存在一个长度为10的数组里面,然后将这个数组与前面32个数字分解的数组去对比,只要相等,就符合;

但是,两个数组是否相等比较麻烦,这样,我们又可以把每个数字设为10的多少次方,这样就没必要去比较整个数组是否相等,直接把这组数字用10的多少次方表示出来;比如N=4654,其中有2个4,1个5,1个6,因此可以表示为:\(10^{4}\)+\(10^{4}\)+\(10^{5}\)+\(10^{6}\),这样出来的结果是唯一的,因此可以比较

代码如下:

class Solution {
public:
    bool reorderedPowerOf2(int N)
    {
        long ans;
        ans=count(N);
        for (int i=0;i<32;i++)
        {
            if (count(1<<i)==ans)
            {
                return true;
            }
        }
        return false;
    }
    long count(int N)
    {
        long ans = 0;
        while(N>0)
        {
            ans+=pow(10,N%10);
            N/=10;
        }
        return ans;
    }
};

原文地址:https://www.cnblogs.com/qiulinzhang/p/9514321.html

时间: 2024-08-04 06:50:52

[Math_Medium] 869. Reordered Power of 2的相关文章

869. Reordered Power of 2

Starting with a positive 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 in a way such that the resulting number is a power of 2. Example

[LeetCode] Reordered Power of 2 重新排序为2的倍数

Starting with a positive 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 in a way such that the resulting number is a power of 2. Example

【LeetCode】数学(共106题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [2]Add Two Numbers [7]Reverse Integer [8]String to Integer (atoi) [9]Palindrome Number [12]Integer to Roman [13]Roman to Integer [29]Divide Two Integers [43]Multiply Strings [50]Pow(x,

[LeetCode] Power of Three

Given an integer, write a function to determine if it is a power of three. Follow up:Could you do it without using any loop / recursion? 判断一个数是否是3的幂,则这个数a的所有的约数都是3的幂,如果一个数b小于这个数a且是3的幂,则这个数b一定是a的约数.所以找出3的最大的幂,然后用这个数对n取余即可. class Solution { public: boo

poj 3134 Power Calculus(迭代加深dfs+强剪枝)

Description Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multiplications: x2 = x × x, x3 = x2 × x, x4 = x3 × x, …, x31 = x30 × x. The operation of squaring can be appreciably shorten the sequence of multiplications.

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分)

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define MAX_SIZE 30 #define CLR( a, b ) memset( a, b, sizeof(a) ) int MOD = 0; int n, k; st

Power BI教程_Power BI数据分析快速上手及案例实战

Power BI数据分析快速上手及案例实战 课程学习地址:http://www.xuetuwuyou.com/course/194 课程出自学途无忧网:http://www.xuetuwuyou.com 课程简介 本课程在<Power BI 数据分析快速上手>基础上结合大量的实例,深入讲解PowerBI中看似难懂的各种概念.操作, 并结合行业中的典型案例贯穿了从初级的数据透视表工具.数据透视表选项.数据透视表的刷新.数据透视表中的排序,到中级的动 态数据透视表的创建.数据透视表函数 GETPI

leetcode 231. Power of Two

Given an integer, write a function to determine if it is a power of two. class Solution { public: bool isPowerOfTwo(int n) { int num = 0; while (n > 0) { if (n &1) num++; n/=2; } if (num == 1) return true; return false; } };

[LeetCode] Power of Two

Given an integer, write a function to determine if it is a power of two. 判断一个整数是否是2的幂.如果一个数是2的幂,则这个数的二进制表示中,最高位是1,其他位都是0.这个数-1的二进制位全是1.所以我们可以利用这个规律对两个数取与进行判断. class Solution { public: bool isPowerOfTwo(int n) { if (n <= 0) return false; return !(n &