LC 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 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
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的更多相关文章
- 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 ...
- lc面试准备:Power of Two
1 题目 Given an integer, write a function to determine if it is a power of two. 接口 boolean isPowerOfTw ...
- [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 ...
- 【Leetcode周赛】从contest-91开始。(一般是10个contest写一篇文章)
Contest 91 (2018年10月24日,周三) 链接:https://leetcode.com/contest/weekly-contest-91/ 模拟比赛情况记录:第一题柠檬摊的那题6分钟 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- ARMA(p,q)模型数据的产生
一.功能 产生自回归滑动平均模型\(ARMA(p,q)\)的数据. 二.方法简介 自回归滑动平均模型\(ARMA(p,q)\)为 \[ x(n)+\sum_{i=1}^{p}a_{i}x(n-i)=\ ...
- vue 之this.$router.push、replace、go的区别
一.this.$router.push 说明:跳转到指定URL,向history栈添加一个新的记录,点击后退会返回至上一个页面 使用: this.$router.push('/index') this ...
- 关于单例模式getInstance()的使用
/** * 对象的实例化方法,也是比较多的,最常用的方法是直接使用new,而这是最普通的,如果要考虑到其它的需要,如单实例模式,层次间调用等等. * 直接使用new就不可以实现好的设计好,这时候需要 ...
- Tomcat conf/server.xml 配置项详解
本文参考来源:https://blog.csdn.net/a314368439/article/details/60132783# <Server port="8005" s ...
- C++-cin与scanf cout与printf效率问题
http://blog.csdn.net/l2580258/article/details/51319387 void cin_read_nosync() { freopen("data.t ...
- django-session的使用---文件session型
3.文件Session a. 配置 settings.py SESSION_ENGINE = 'django.contrib.sessions.backends.file' # 引擎 ...
- BZOJ 1036 [ZJOI2008]树的统计Count 动态维护树上求和与求最大值 LCT板题
模板,也可以用树链剖分+线段树做O(nlog2)O(nlog^2)O(nlog2) 用LCT做O(nlog)O(nlog)O(nlog)在乘上一个大于30的常数-然后LCT比树剖慢一倍- CODE # ...
- 题解 [BZOJ1832][AHOI2008] 聚会
题面 解析 首先对于其中的两个点\(x,y\)最近的点显然就是他们的\(lca\)(我们把它设为\(p1\)), 然后考虑第三个点\(z\)与\(p1\)的\(lca,p2\). 有以下几种情况: \ ...
- i3wm脚本
exec 执行命令 --no-startup-id 有些脚本或者程序不支持启动通知,不加命令,鼠标会长时间空转,60秒左右 exec_always 每次重启i3,使用该命令启动的程序都会重新执行一次, ...
- Java SE练习 - 对dom4j解析、反射的综合练习
原 Java SE练习 - 对dom4j解析.反射的综合练习 2017年12月13日 14:41:07 都说名字长不会被发现 阅读数 138 版权声明:本文为博主原创文章,遵循CC 4.0 by-sa ...