【LeetCode】869. Reordered Power of 2 解题报告(Python & C++)
作者: 负雪明烛
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 <= 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++)的更多相关文章
- 【LeetCode】342. Power of Four 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 位运算 函数法 日期 [LeetCode ...
- 【LeetCode】231. Power of Two 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二进制 位运算 判断是不是最大2的幂的因数 判断因子 ...
- 【LeetCode】326. Power of Three 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 循环 递归 取对数 判断是不是最大3的倍数的因子 日 ...
- leetcode 869. Reordered Power of 2
function reorderedPowerOf2(N) { var a1 = N.toString().split('') a1.sort((a, b) => a.localeCompare ...
- 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...
- 【LeetCode】760. Find Anagram Mappings 解题报告
[LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
随机推荐
- a.out的由来
用过linux的都知道,在linux下编译链接程序,如果不加-o参数,生成的binary代码的名字都是默认的a.out.一不小心,a.out还会覆盖上次其他code生成的binary代码. a.out ...
- 使用 CliWrap 让C#中的命令行交互举重若轻
在代码中进行命令行交互是一个很常见的场景, 特别是在一些CI CD 自动化流程中, 在这之前我们会使用 System.Diagnostics.Process API, 现在有一个更灵活的工具 CliW ...
- 零基础学习java------23---------动态代理,ip,url案例
1. 动态代理 2. ip,url案例 给定的access.log是电信运营商的用户上网数据,第一个字段是时间, 第二个字段是ip地址,第三个字段是访问的网站,其他字段可以忽略不计. 第一个字段是网段 ...
- nodejs-os模块
JavaScript 标准参考教程(alpha) 草稿二:Node.js os模块 GitHub TOP os模块 来自<JavaScript 标准参考教程(alpha)>,by 阮一峰 ...
- 关于vue-cli中-webkit-flex-direction: column失效问题
我最近在用vue-cli更新项目,在我引入layer.css后会报错并且使用弹性盒时查看元素的时候没有-webkit-flex-direction: column这个属性会失效 这个本身就不打算给di ...
- 了解 Linkerd Service Mesh 架构
从较高的层次上看,Linkerd 由一个控制平面(control plane) 和一个 数据平面(data plane) 组成. 控制平面是一组服务,提供对 Linkerd 整体的控制. 数据平面由在 ...
- 什么是 IP 地址 – 定义和解释
IP 地址定义 IP 地址是一个唯一地址,用于标识互联网或本地网络上的设备.IP 代表"互联网协议",它是控制通过互联网或本地网络发送的数据格式的一组规则. 本质上,IP 地址是允 ...
- final&static
final 1.final修饰类,那么该类不能有子类,那么也就没有子类重写父类的方法,也就没有多态 2.final修饰成员变量,那么成员变量要么显式赋值(用第一种),要么在构造方法中赋值 无论哪一种, ...
- 【编程思想】【设计模式】【结构模式Structural】front_controller
Python版 https://github.com/faif/python-patterns/blob/master/structural/front_controller.py #!/usr/bi ...
- 【Spark】【RDD】从HDFS创建RDD
1.在HDFS根目录下创建目录(姓名学号) hdfs dfs -mkdir /zwj25 hdfs dfs -ls / 访问 http://[IP]:50070 2.上传本地文件到HDFS hdfs ...