869. Reordered Power of 2
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
trueif 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: trueExample 2:
Input: 10
Output: falseExample 3:
Input: 16
Output: trueExample 4:
Input: 24
Output: falseExample 5:
Input: 46
Output: true
Note:
1 <= N <= 10^9
Approach #1: Math. [Java]
class Solution {
public boolean reorderedPowerOf2(int N) {
int c = count(N);
for (int i = 0; i < 32; ++i) {
if (count(1 << i) == c) return true;
}
return false;
}
public int count(int x) {
int ret = 0;
for (; x > 0; x /= 10)
ret += (int)Math.pow(10, x % 10);
return ret;
}
}
Analysis:
The way that use / and % to count the digit is awesome.
Reference:
https://leetcode.com/problems/reordered-power-of-2/discuss/149843/C%2B%2BJavaPython-Straight-Forward
869. Reordered Power of 2的更多相关文章
- LC 869. Reordered Power of 2
Starting with a positive integer N, we reorder the digits in any order (including the original order ...
- 【LeetCode】869. Reordered Power of 2 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计每位数字出现的次数 日期 题目地址:http ...
- leetcode 869. Reordered Power of 2
function reorderedPowerOf2(N) { var a1 = N.toString().split('') a1.sort((a, b) => a.localeCompare ...
- [LeetCode] Reordered Power of 2 重新排序为2的倍数
Starting with a positive integer N, we reorder the digits in any order (including the original order ...
- [Swift]LeetCode869. 重新排序得到 2 的幂 | Reordered Power of 2
Starting with a positive integer N, we reorder the digits in any order (including the original order ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
- 【Leetcode周赛】从contest-91开始。(一般是10个contest写一篇文章)
Contest 91 (2018年10月24日,周三) 链接:https://leetcode.com/contest/weekly-contest-91/ 模拟比赛情况记录:第一题柠檬摊的那题6分钟 ...
随机推荐
- 如何用Eggjs从零开始开发一个项目(3)
上一篇中我们编写了用户注册登录.登录的代码,学习了如何进行用户的认证(JWT),如何安全地存储用的密码(hash).这一篇我们有以下2个任务: 获取token中的数据: 通过model来同步数据库. ...
- Tango with django 1.9 中文——3.Django基础
让我们开始运用Django.本章主要是给你一个关于创建新项目和新应用过程的概览.在本章的末尾,你将建立起一个简单的由Django驱动的网站. 3.1 配置测试 让我们测试以下你的Python和Djan ...
- LeetCode-祖父节点值为偶数的结点值之和
祖父节点值为偶数的结点值之和 LeetCode-1315 这题稍微难度有点大,但是仔细思考还是可以找到思路的. 因为只需要找到祖父节点这最上两层,所以可以带一个参数记录一下祖父节点是否是偶数,以及父节 ...
- MySQL 表的约束与数据库设计
DQL 查询语句 排序 # 单列排序 * 只按某一个字段进行排序,单列排序 # 组合排序 * 同时对多个字段进行排序,如果第1个字段相等,则按照第2个字段排序,依次类推 * 语法: # 具体操作 * ...
- linux_MYSQL 数据库自动备份并压缩和删除历史备份
1. 创建shell脚本 #! /bin/bash# MySQL用户user="root"# MySQL密码userPWD="123456789"# 需要定时备 ...
- 读 Kafka 源码写优雅业务代码:配置类
这个 Kafka 的专题,我会从系统整体架构,设计到代码落地.和大家一起杠源码,学技巧,涨知识.希望大家持续关注一起见证成长! 我相信:技术的道路,十年如一日!十年磨一剑! 往期文章 Kafka 探险 ...
- 策略模式在PHP业务代码的实践
[大话设计模式]-- 策略者模式(Strategy):它定义了算法家族,分别封装起来,让他们之间可以互相替换,此模式让算法的变法,不会影响到使用算法的客户. 策略模式的核心就是屏蔽内部策略算法,内部的 ...
- python对一个文本的解析
# 定义Tag的签注 controlAreaStart ="<ControlArea::黄冈>" controlAreaEnd = "</Control ...
- Django中间件(中间件版登陆验证、访问频率限制)
一.介绍 官方的说法:中间件是一个用来处理Django的请求和响应的框架级别的钩子.它是一个轻量.低级别的插件系统,用于在全局范围内改变Django的输入和输出.每个中间件组件都负责做一些特定的功能. ...
- Paint Chain HDU - 3980
题目链接:https://vjudge.net/problem/HDU-3980 题意:由n个石头组成的环,每次只能取连续的M个,最后不能取得人输. 思路:这样就可以先把它变成链,然后在链上枚举取m个 ...