【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 ...
随机推荐
- Linux—export命令查看、修改用户环境变量
Linux export 命令用于设置或显示环境变量. 在 shell 中执行程序时,shell 会提供一组环境变量. export 可新增,修改或删除环境变量,供后续执行的程序使用. export ...
- 简单mvc框架核心笔记
简单mvc框架核心笔记 看了thinkphp5的源码,模仿写了一个简单的框架,有一些心得笔记,记录一下 1.目录结构 比较简单,没有tp那么复杂,只是把需要的核心类写了一些. 核心类库放在mykj里, ...
- kubernetes部署kube-controller-manager服务
本文档介绍部署高可用 kube-controller-manager 集群的步骤. 该集群包含 3 个节点,启动后将通过竞争选举机制产生一个 leader 节点,其它节点为阻塞状态.当 leader ...
- 数据库时间和 java 时间不一致解决方案
java添加 date 到数据库,时间不一致 使用 date 添加到数据库,数据库显示的时候和date时间相差 8 个小时,这是由于 mysql 上的时区的问题,这里有两个解决方案: 方案一: 设置数 ...
- 复制virtualbox虚拟硬盘
D:\VirtualBox\VBoxManage.exe clonevdi F:\virtualbox\rac1\rac1.vdi F:\virtualbox\rac2\rac2.vdi 虚拟机软件安 ...
- mybatis缓存+aop出现的问题
在对某些特殊数据进行转换时,getOne方法后执行fieleInfoHandle进行转换,如果直接使用fixedTableData进行操作,没有后续的二次调用这样是没问题的,但是在后面当执行完upda ...
- PhoneGap本地将html打包成安卓App
PhoneGap的在线打包有大小限制,超过30M的包无法在线打包.当然,可以把包里面的图片.声音文件去掉,然后打包.下载以后,解包,重新打包并签名.蛮麻烦的. 本地打包的简单方法如下: 下载安装Jav ...
- activiti工作流引擎
参考文章 Activiti-5.18.0与springMvc项目集成和activiti-explorer单独部署Web项目并与业务数据库关联方法(AutoEE_V2实现方式) https://blog ...
- @Order注解使用
注解@Order或者接口Ordered的作用是定义Spring IOC容器中Bean的执行顺序的优先级,而不是定义Bean的加载顺序,Bean的加载顺序不受@Order或Ordered接口的影响: @ ...
- “==” 和 equals()的区别
※ "==" 和 equals()的区别 ※ == :比较. 基本数据类型比较的是值:. 引用类型比较的是地址值. ※ equals(Object o):1)不能比较基本数据类型, ...