作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/reordered-power-of-2/description/

题目描述

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

题目大意

判断一个数字经过重新组织各个数字的顺序后,能否组成2的幂。

解题方法

字典统计每位数字出现的次数

最初的想法是通过DFS的方式作组合,但是可能会超时。

想到N只有九位数字,那么在这个范围内的2的幂,应该不多。可以看N和这些2的幂是否拥有相同的数字就好了。使用Counter方法,判断N里的数字和2的幂数字是否相同。

python代码如下:

class Solution(object):
def reorderedPowerOf2(self, N):
"""
:type N: int
:rtype: bool
"""
c = collections.Counter(str(N))
return any(c == collections.Counter(str(1 << i)) for i in xrange(32))

C++里面的字典由于做了运算符重载,是可以直接进行==或者!=比较的,具体的比较方式是先比较字典的元素个数是否相等,如果相等再依次拿出每一个元素看在第二个字典中是否存在。代码如下:

class Solution {
public:
bool reorderedPowerOf2(int N) {
unordered_map<char, int> c = count(N);
for (int i = 0; i < 32; ++i) {
auto counti = count(1 << i);
if (counti == c)
return true;
}
return false;
}
unordered_map<char, int> count(int N) {
unordered_map<char, int> c;
while (N != 0) {
++c[N % 10];
N /= 10;
}
return c;
}
};

参考资料:

https://leetcode.com/problems/reordered-power-of-2/discuss/149843/C++JavaPython-Straight-Forward

日期

2018 年 9 月 6 日 —— 作息逐渐规律。
2019 年 1 月 26 日 —— 周末加班

【LeetCode】869. Reordered Power of 2 解题报告(Python & C++)的更多相关文章

  1. 【LeetCode】342. Power of Four 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 位运算 函数法 日期 [LeetCode ...

  2. 【LeetCode】231. Power of Two 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二进制 位运算 判断是不是最大2的幂的因数 判断因子 ...

  3. 【LeetCode】326. Power of Three 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 循环 递归 取对数 判断是不是最大3的倍数的因子 日 ...

  4. leetcode 869. Reordered Power of 2

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

  5. 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...

  6. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  7. 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...

  8. 【LeetCode】760. Find Anagram Mappings 解题报告

    [LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find ...

  9. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

随机推荐

  1. shell 除法和格式化输出printf

    相关知识的补充: printf命令模仿C程序库里的printf()程序.printf由POSIX标准所定义,因此使用printf的脚本比使用echo有着更好的移植性. printf使用引用文本或者空格 ...

  2. TensorFlow 2.0 深度学习实战 —— 浅谈卷积神经网络 CNN

    前言 上一章为大家介绍过深度学习的基础和多层感知机 MLP 的应用,本章开始将深入讲解卷积神经网络的实用场景.卷积神经网络 CNN(Convolutional Neural Networks,Conv ...

  3. ceph安装部署

    环境准备 测试环境是4台虚拟机,所有机器都是刚刚安装好系统(minimal),只配置完网卡和主机名的centos7.7,每个osd增加一块磁盘,/dev/sdb ceph-admin ---- adm ...

  4. Learning Spark中文版--第四章--使用键值对(1)

      本章介绍了如何使用键值对RDD,Spark中很多操作都基于此数据类型.键值对RDD通常在聚合操作中使用,而且我们经常做一些初始的ETL(extract(提取),transform(转换)和load ...

  5. Netty实现Socket

    Netty实现Socket 从Java1.4开始, Java引入了non-blocking IO,简称NIO.NIO与传统socket最大的不同就是引入了Channel和多路复用selector的概念 ...

  6. linux允许直接以root身份ssh登录

    1. sudo su - 2. vim /etc/ssh/sshd_config 3. let "PermitRootLogin" equal yes 4. :wq 5. serv ...

  7. OC-ARC,类扩展,block

    总结 标号 主题 内容 一 autorelease autorelease基本概念/自动释放池/autorelease基本使用 二 autorelease注意事项 注意点/应用场景 三 ARC 什么是 ...

  8. Spring 文档汇总

    Spring Batch - Reference Documentation Spring Batch 参考文档中文版 Spring Batch 中文文档 Table 2. JdbcCursorIte ...

  9. 阿里巴巴Java开发手册摘要(二)

    MySql数据库 一建表规约 1.表达是与否概念的字段,必须使用is_xxx的命名方式,数据类型是unsigned tinyint(1:是,0否) 正例:表达逻辑删除的字段名is_deleted,1表 ...

  10. java通过JDBC访问数据库(最基本的查询)

    一.步骤介绍 1.通过Class.forName()加载驱动: 2.通过DriverManager.getConnection()获取Conncetion连接对象: 3.创建Statement对象传递 ...