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 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- SSISDB8:查看SSISDB记录Package执行的消息
在执行Package时,SSISDB都会创建唯一的OperationID 和 ExecutionID,标识对package执行的操作和执行实例(Execution Instance),并记录opera ...
- Django安装和介绍
在CMD和pycharm的安装方法. 先说CMD的安装方法 1,使用pip3 install django 2,进入c:python\Scripts 3,django-admin.exe startp ...
- pip安装超时解决方案
1 安装的后面 用-i接一些国内的镜像,下面这个是清华的,亲测比较快 pip install apache-airflow -i https://pypi.tuna.tsinghua.edu.cn/s ...
- C# Winfrom GDI+ 自定义控件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; u ...
- JS中数组初始化以及赋值
.指定长度,然后初始化 ); ;index < ;index++){ vArray[index] = index; } 2.不指定长度,然后初始化 var vArray = new Array( ...
- PAT Basic 1083 是否存在相等的差 (20 分)
给定 N 张卡片,正面分别写上 1.2.…….N,然后全部翻面,洗牌,在背面分别写上 1.2.…….N.将每张牌的正反两面数字相减(大减小),得到 N 个非负差值,其中是否存在相等的差? 输入格式: ...
- python __file__ is not defined 解决方法
python __file__ is not defined 解决方法 __file__ 是在python module 被导入的时候生成的一个变量,所以在 __file__ 不能被使用,但是又想获取 ...
- goproxy
go env -w GOPROXY=https://goproxy.cn,directgo env -w GO111MODULE=ongo env -w GOBIN=$HOME/bin (可选)go ...
- MyEclipse运行项目出现 The user operation is waiting for "Building workspace" to complete
如图所示 解决方式 1.选择菜单栏的“Project”,然后把菜单栏中“Build Automatically”前面的对钩去掉. 2.当你修改或添加代码后,选择菜单栏的“Project”,然后选择菜单 ...
- 【csp模拟赛5】加减法--宽搜维护联通快
题目大意: 一开始想用并查集,发现很难维护联通块的代表元素,所以用了宽搜,开数组会炸,所以开一个优先队列维护,每扫完一个联通块,统计答案,清空优先队列,!!千万记住注意数组的大小!!! 代码: #in ...