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 1:

Input: 1
Output: true

Example 2:

Input: 10
Output: false

Example 3:

Input: 16
Output: true

Example 4:

Input: 24
Output: false

Example 5:

Input: 46
Output: true

Note:

  1. 1 <= N <= 10^9

Approach #1: Math. [Java]

class Solution {
    public boolean reorderedPowerOf2(int N) {
        int c = count(N);
        for (int i = 0; i < 32; ++i) {
            if (count(1 << i) == c) return true;
        }
        return false;
    }

    public int count(int x) {
        int ret = 0;
        for (; x > 0; x /= 10)
            ret += (int)Math.pow(10, x % 10);
        return ret;
    }
}

Analysis:

The way that use / and % to count the digit is awesome.

  

Reference:

https://leetcode.com/problems/reordered-power-of-2/discuss/149843/C%2B%2BJavaPython-Straight-Forward

原文地址:https://www.cnblogs.com/ruruozhenhao/p/10897015.html

时间: 2024-10-11 12:30:48

869. Reordered Power of 2的相关文章

[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 num

[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 &