作者: 负雪明烛
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. Python基础之基本运算符

    目录 1. 算数运算符 2. 比较运算符 3. 赋值运算符 4. 逻辑运算符 5. 身份运算 6. 运算符优先级 1. 算数运算符 常用算术运算符使用方法如下: x = 5 y = 2 a = x + ...

  2. jquery操作html中图片宽高自适应

    在网站制作中如果后台上传的图片不做宽高限制,在前台显示的时候,经常会出现图片变形,实用下面方法可以让图片根据宽高自适应,不论是长图片或者高图片都可以完美显示. $("#myTab0_Cont ...

  3. 移动测试(web和app)及app测试实战

    移动测试androidiosapp上 原生GUI 混合应用H5 web端兼容性浏览器测试需要的内容:safari 浏览器edge浏览器ie11浏览器firefox浏览器chrome浏览器 国内360浏 ...

  4. A Child's History of England.7

    After the death of Ethelbert, Edwin, King of Northumbria [公元616年,隋朝末年], who was such a good king tha ...

  5. A Child's History of England.35

    The other two clung to the yard for some hours. At length the young noble said faintly, 'I am exhaus ...

  6. Celery进阶

    Celery进阶 在你的应用中使用Celery 我们的项目 proj/__init__.py   /celery.py   /tasks.py 1 # celery.py 2 from celery ...

  7. Scala(六)【模式匹配】

    目录 一.基本语法 二.匹配固定值 三.守卫 四.匹配类型 五.匹配集合 1.Array 2.List 3.元祖 4.对象和样例类 六.偏函数 七.赋值匹配 八.for循环匹配 一.基本语法 在匹配某 ...

  8. 零基础学习java------30---------wordCount案例(涉及到第三种多线程callable)

    知识补充:多线程的第三种方式 来源:http://www.threadworld.cn/archives/39.html 创建线程的两种方式,一种是直接继承Thread,另外一种就是实现Runnabl ...

  9. Shell脚本字符串截取方法总结

    Shell脚本8种字符串截取方法总结转自:https://www.cnblogs.com/ralphdc/p/8032335.html Linux 的字符串截取很有用.有八种方法.假设有变量 var= ...

  10. ORACLE 加大日志文件

    --新建临时日志文件alter database add logfile group 4 ('/u01/app/oracle/oradata/orcl/redo04.log') size 10m;al ...