博客
关于我
869. Reordered Power of 2
阅读量:416 次
发布时间:2019-03-06

本文共 895 字,大约阅读时间需要 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 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: 1Output: true

Example 2:

Input: 10Output: false

Example 3:

Input: 16Output: true

Example 4:

Input: 24Output: false

Example 5:

Input: 46Output: true

 

Note:

  1. 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:

 

转载地址:http://udtuz.baihongyu.com/

你可能感兴趣的文章
痞子衡嵌入式:串口调试工具pzh-com诞生记(2)- 界面构建(wxFormBuilder3.8.0)
查看>>
elementUi源码解析(1)--项目结构篇
查看>>
自动遍历测试之Monkey工具
查看>>
Nmap扫描工具介绍
查看>>
算法笔记:递归、动态规划
查看>>
Pytest插件开发
查看>>
常用Windows 快捷键
查看>>
linux命令-压缩与打包
查看>>
ORACLE 11g 生产中高水位线(HWM)处理
查看>>
centos 6.x 编译安装 pgsql 9.6
查看>>
weblogic 服务器部署SSL证书
查看>>
Oracle Orion tool check io(ORACLE Orion 工具查看以及校验IO)
查看>>
oracle 11g not in 与not exists 那个高效?
查看>>
Linux 安装Redis 5.0(以及参数调优)
查看>>
html5 Game开发系列文章之 零[开篇]
查看>>
为什么阿里巴巴建议集合初始化时,指定集合容量大小
查看>>
原创 | 我被面试官给虐懵了,竟然是因为我不懂Spring中的@Configuration
查看>>
为什么阿里巴巴要求谨慎使用ArrayList中的subList方法
查看>>
Redis不是一直号称单线程效率也很高吗,为什么又采用多线程了?
查看>>
基于Python的Appium环境搭建合集
查看>>