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
 
Runtime: 11 ms, faster than 52.38% of Java online submissions for Reordered Power of 2.
class Solution {
private char[] Ncarr;
public boolean ispermutation(String astr){
char[] acarr = astr.toCharArray();
Arrays.sort(acarr);
for(int i=; i<acarr.length; i++){
if(acarr[i] != Ncarr[i]) return false;
}
return true;
} public boolean reorderedPowerOf2(int N) {
if(N == || N == ) return true;
String Nstr = Integer.toString(N);
Ncarr = Nstr.toCharArray();
Arrays.sort(Ncarr);
int digitN = Nstr.length();
int base = ;
List<String> tmplist = new ArrayList<>();
while(true){
String tmp = Integer.toString(base);
if(tmp.length() == digitN) tmplist.add(tmp);
if(((base >> ) & ) == ) break;
if(tmp.length() > digitN) break;
base <<= ;
}
for(String x : tmplist){
if(ispermutation(x)) return true;
}
return false;
} }
 

LC 869. Reordered Power of 2的更多相关文章

  1. 869. Reordered Power of 2

    Starting with a positive integer N, we reorder the digits in any order (including the original order ...

  2. 【LeetCode】869. Reordered Power of 2 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计每位数字出现的次数 日期 题目地址:http ...

  3. leetcode 869. Reordered Power of 2

    function reorderedPowerOf2(N) { var a1 = N.toString().split('') a1.sort((a, b) => a.localeCompare ...

  4. [LeetCode] Reordered Power of 2 重新排序为2的倍数

    Starting with a positive integer N, we reorder the digits in any order (including the original order ...

  5. lc面试准备:Power of Two

    1 题目 Given an integer, write a function to determine if it is a power of two. 接口 boolean isPowerOfTw ...

  6. [Swift]LeetCode869. 重新排序得到 2 的幂 | Reordered Power of 2

    Starting with a positive integer N, we reorder the digits in any order (including the original order ...

  7. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  8. 【Leetcode周赛】从contest-91开始。(一般是10个contest写一篇文章)

    Contest 91 (2018年10月24日,周三) 链接:https://leetcode.com/contest/weekly-contest-91/ 模拟比赛情况记录:第一题柠檬摊的那题6分钟 ...

  9. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

随机推荐

  1. 深入SpringBoot注解原理及使用

    首先,先看SpringBoot的主配置类: @SpringBootApplication public class StartEurekaApplication { public static voi ...

  2. late_initcall 替换 module_init

    今天在调试pwm驱动程序的时候,在__init函数中调用pwm_init后,则以太网不可用.pwm_init放在设备文件的open函数中,则系统正常运行. 这当中的区别就是硬件初始化函数pwm_ini ...

  3. 车型识别API调用与批量分类车辆图片

    版权声明:本文为博主原创文章,转载 请注明出处 https://blog.csdn.net/sc2079/article/details/82189824 9月9日更:博客资源下载:链接: https ...

  4. PHP把数组按指定的个数分隔

    PHP把数组按指定的个数分隔 假设数组为array(‘1’,‘2’,‘3’,‘4’,‘5’,‘6’); 想把它分割成四个,那么结果为array(‘0’ => [‘1’,‘2’],‘1’ => ...

  5. dynamic类型

    dynamic类型在运行时做类型检查 可用于变量类型.方法参数和返回值类型 示例 dynamic person = new Student { Name = "张三", Age = ...

  6. Kubernetes1.16下部署Prometheus+node-exporter+Grafana+AlertManager 监控系统

    Prometheus 持久化安装 我们prometheus采用nfs挂载方式来存储数据,同时使用configMap管理配置文件.并且我们将所有的prometheus存储在kube-system #建议 ...

  7. NOI2018 你的名字——SAM+线段树合并

    题目链接在这里洛谷/LOJ 题目大意 有一个串\(S\),每次询问给你一个串\(T\),两个数\(L\)和\(R\),问你\(T\)有多少个本质不同的子串不是\(S[L,R]\)的子串 SOLUTIO ...

  8. C# 运算符和类型强制转换(6) 持续更新

    C#支持的运算符 https://msdn.microsoft.com/zh-cn/library/6a71f45d(v=vs.140).aspx checked 和 unchecked ; b++; ...

  9. mysql 表字段与关键字相同的话

    desc is a reserved keyword (short for DESCENDING in ORDER BY). Enlose it into backticks: INSERT INTO ...

  10. 2018多校第九场 HDU 6416 (DP+前缀和优化)

    转自:https://blog.csdn.net/CatDsy/article/details/81876341 #include <bits/stdc++.h> using namesp ...