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

这道题说是给了我们一个正整数N,让对各位上的数字进行重新排序,但是要保证最高位上不是0,问能否变为2的指数。刚开始的时候博主理解错了,以为是对N的二进制数的各位进行重排序,但除了2的指数本身,其他数字怎么也组不成2的指数啊,因为2的指数的二进制数只有最高位是1,其余都是0。后来才发现,是让对N的十进制数的各位上的数字进行重排序,比如 N=46,那么换个位置,变成 64,就是2的指数了。搞清了题意后,就可以开始解题了,由于N给定了范围,在 [1, 1e9] 之间,所以其调换位数能组成的二进制数也是有范围的,为 [2^0, 2^30] 之间,这样的话,一个比较直接的解法就是,现将整数N转为字符串,然后对字符串进行排序。然后遍历所有可能的2的指数,将每个2的指数也转为字符串并排序,这样只要某个排序后的字符串跟之前由N生成的字符串相等的话,则表明整数N是符合题意的,参见代码如下:


解法一:

class Solution {
public:
bool reorderedPowerOf2(int N) {
string str = to_string(N);
sort(str.begin(), str.end());
for (int i = 0; i < 31; ++i) {
string t = to_string(1 << i);
sort(t.begin(), t.end());
if (t == str) return true;
}
return false;
}
};

下面这种方法没有将数字转为字符串并排序,而是使用了另一种比较巧妙的方法来实现类似的功能,是通过对N的每位上的数字都变为10的倍数,并相加,这样相当于将N的各位的上的数字都加码到了10的指数空间,而对于所有的2的指数,进行相同的操作,只要某个加码后的数字跟之前整数N的处理后的数字相同,则说明N是符合题意的。需要注意的是,为了防止整型移除,加码后的数字用长整型来表示即可,参见代码如下:


解法二:

class Solution {
public:
bool reorderedPowerOf2(int N) {
long sum = helper(N);
for (int i = 0; i < 31; i++) {
if (helper(1 << i) == sum) return true;
}
return false;
}
long helper(int N) {
long res = 0;
for (; N; N /= 10) res += pow(10, N % 10);
return res;
}
};

讨论:对于这种验证数字的问题,总是有穷举法出现,参见[这个帖子](https://leetcode.com/problems/reordered-power-of-2/discuss/159513/C%2B%2B-0ms-beats-100),是能把考官气死的方法,哈哈~


Github 同步地址:

https://github.com/grandyang/leetcode/issues/869

参考资料:

https://leetcode.com/problems/reordered-power-of-2/

https://leetcode.com/problems/reordered-power-of-2/discuss/159513/C%2B%2B-0ms-beats-100

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

https://leetcode.com/problems/reordered-power-of-2/discuss/151949/Simple-Java-Solution-Based-on-String-Sorting

[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

[LeetCode] Reordered Power of 2 重新排序为2的倍数的更多相关文章

  1. leetcode 326. Power of Three(不用循环或递归)

    leetcode 326. Power of Three(不用循环或递归) Given an integer, write a function to determine if it is a pow ...

  2. [LeetCode] 231. Power of Two 2的次方数

    Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1 Output: ...

  3. [LeetCode] 342. Power of Four 4的次方数

    Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...

  4. LC 869. Reordered Power of 2

    Starting with a positive integer N, we reorder the digits in any order (including the original order ...

  5. [Swift]LeetCode869. 重新排序得到 2 的幂 | Reordered Power of 2

    Starting with a positive integer N, we reorder the digits in any order (including the original order ...

  6. 【LeetCode】869. Reordered Power of 2 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计每位数字出现的次数 日期 题目地址:http ...

  7. leetcode 869. Reordered Power of 2

    function reorderedPowerOf2(N) { var a1 = N.toString().split('') a1.sort((a, b) => a.localeCompare ...

  8. leetcode:Power of Two

    Given an integer, write a function to determine if it is a power of two. 分析:这道题让我们判断一个数是否为2的次方数(而且要求 ...

  9. [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four

    这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...

随机推荐

  1. 分布式系列十一: Redis进阶

    分布式锁 数据库 数据库是使用唯一索引不允许重复的特性(或自定义实现如乐观锁). 但持有锁的进程如果释放锁时异常则容易导致死锁. zookeeper 使用临时节点, watcher可以获得节点被删除的 ...

  2. Linux中查看端口占用情况

    1.lsof -i:端口号 用于查看某一端口的占用情况,比如查看8000端口的使用情况: # lsof -i:8000 2.netstat -tunlp | grep 端口号,用于查看指定的端口号的进 ...

  3. git知识总结-2.git基本操作之原理说明

    0.前言 本文主要对git常用命令的工作原理做一个详细的说明,常用命令主要包括: 1.git add git add相关命令很简单,主要实现将工作区修改的内容提交到暂存区,交由git管理. 2. gi ...

  4. Grunt 实战

    专题截图:(注:这个截图没啥意义) 项目截图: 目录讲解: app/        //开发目录; c/     //开发编译完成css文件夹; i/     //开发img文件夹; j/     / ...

  5. ptmalloc内存分配释放

    出处 分配: 1)获取分配区的锁,为了防止多个线程同时访问同一个分配区,在进行分配之前需要取得分配区域的锁.线程先查看线程私有实例中是否已经存在一个分配区,如果存在尝试对该分配区加锁,如果加锁成功,使 ...

  6. TableView+Button

    local MainScene = class("MainScene", cc.load("mvc").ViewBase) function MainScene ...

  7. python正则表达式基础,以及pattern.match(),re.match(),pattern.search(),re.search()方法的使用和区别

    正则表达式(regular expression)是一个特殊的字符序列,描述了一种字符串匹配的模式,可以用来检查一个字符串是否含有某种子字符串. 将匹配的子字符串替换或者从某个字符串中取出符合某个条件 ...

  8. 利用 Google Chart API 生成二维码大小不一致

    大小不一致是由于 chl  参数内容不一样导致的,而 chs 参数只能指定生成图片的大小,不能指定生成具体二维码大小. 比如:https://chart.googleapis.com/chart?ch ...

  9. python vs C++ 类

    1. 什么是动态语言(wikipedia) 在运行时,可以进行一些操作(静态语言在编译时执行),比如扩展对象的定义.修改类型等 2. 定义类和创建对象 C++ python class A{ publ ...

  10. java程序设计第二次作业