当前位置:网站首页>leetcode 869. Reordered Power of 2 | 869. 重新排序得到 2 的幂(状态压缩)
leetcode 869. Reordered Power of 2 | 869. 重新排序得到 2 的幂(状态压缩)
2022-07-08 00:37:00 【寒泉Hq】
题目
https://leetcode.com/problems/reordered-power-of-2/
题解
class Solution {
public boolean reorderedPowerOf2(int n) {
Set<Long> set = new HashSet<>();
int target = 1;
for (int i = 0; i < 31; i++) {
set.add(compress(target));
target <<= 1;
}
return set.contains(compress(n));
}
public long compress(int n) {
// int拍平成array
// index 0 1 2 3 4 5 6 7 8 9
// num=2566 -> [0,0,1,0,0,1,2,0,0,0]
int[] count = new int[10];
for (char c : String.valueOf(n).toCharArray()) {
count[c - '0']++;
}
// 对array状态压缩
// [0,0,1,0,0,1,2,0,0,0] -> 0010012000
long res = 0;
for (int c : count) {
res *= 10;
res += c;
}
return res;
}
}
边栏推荐
- 如何用Diffusion models做interpolation插值任务?——原理解析和代码实战
- 快手小程序担保支付php源码封装
- Reading notes of Clickhouse principle analysis and Application Practice (7)
- Keras深度学习实战——基于Inception v3实现性别分类
- In depth analysis of ArrayList source code, from the most basic capacity expansion principle, to the magic iterator and fast fail mechanism, you have everything you want!!!
- 关于TXE和TC标志位的小知识
- 软件测试笔试题你会吗?
- Redission源码解析
- 【目标跟踪】|atom
- I don't know. The real interest rate of Huabai installment is so high
猜你喜欢
Application of slip ring in direct drive motor rotor
Android 创建的sqlite3数据存放位置
Kwai applet guaranteed payment PHP source code packaging
Introduction to ADB tools
SQLite3 data storage location created by Android
Can you write the software test questions?
pb9.0 insert ole control 错误的修复工具
静态路由配置全面详解,静态路由快速入门指南
C语言-Cmake-CMakeLists.txt教程
Why did MySQL query not go to the index? This article will give you a comprehensive analysis
随机推荐
What are the types of system tests? Let me introduce them to you
COMSOL --- construction of micro resistance beam model --- final temperature distribution and deformation --- addition of materials
Codeforces Round #633 (Div. 2) B. Sorted Adjacent Differences
How to make enterprise recruitment QR code?
Version 2.0 of tapdata, the open source live data platform, has been released
保姆级教程:Azkaban执行jar包(带测试样例及结果)
Codeforces Round #643 (Div. 2)——B. Young Explorers
Get familiar with XML parsing quickly
为什么更新了 DNS 记录不生效?
PB9.0 insert OLE control error repair tool
《ClickHouse原理解析与应用实践》读书笔记(7)
Codeforces Round #649 (Div. 2)——A. XXXXX
WPF 自定义 写实风 雷达图控件
Sum of submatrix
Usage of hydraulic rotary joint
Apache multiple component vulnerability disclosure (cve-2022-32533/cve-2022-33980/cve-2021-37839)
I don't know. The real interest rate of Huabai installment is so high
Why did MySQL query not go to the index? This article will give you a comprehensive analysis
mysql/mariadb怎样生成core文件
谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题