当前位置:网站首页>leetcode 869. Reordered Power of 2 | 869. Reorder to a power of 2 (state compression)
leetcode 869. Reordered Power of 2 | 869. Reorder to a power of 2 (state compression)
2022-07-08 02:03:00 【Cold spring HQ】
subject
https://leetcode.com/problems/reordered-power-of-2/
Answer key
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 Pai Pingcheng 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']++;
}
// Yes array State compression
// [0,0,1,0,0,1,2,0,0,0] -> 0010012000
long res = 0;
for (int c : count) {
res *= 10;
res += c;
}
return res;
}
}
边栏推荐
- 电路如图,R1=2kΩ,R2=2kΩ,R3=4kΩ,Rf=4kΩ。求输出与输入关系表达式。
- Capability contribution three solutions of gbase were selected into the "financial information innovation ecological laboratory - financial information innovation solutions (the first batch)"
- 《ClickHouse原理解析与应用实践》读书笔记(7)
- Ml self realization /knn/ classification / weightlessness
- adb工具介绍
- Kwai applet guaranteed payment PHP source code packaging
- 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!!!
- 发现值守设备被攻击后分析思路
- burpsuite
- Dataworks duty table
猜你喜欢
QT -- create QT program
Flutter 3.0框架下的小程序运行
MySQL查询为什么没走索引?这篇文章带你全面解析
谈谈 SAP 系统的权限管控和事务记录功能的实现
How to make the conductive slip ring signal better
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!!!
神经网络与深度学习-5- 感知机-PyTorch
剑指 Offer II 041. 滑动窗口的平均值
关于TXE和TC标志位的小知识
adb工具介绍
随机推荐
数据链路层及网络层协议要点
Neural network and deep learning-5-perceptron-pytorch
How to make the conductive slip ring signal better
Apache多个组件漏洞公开(CVE-2022-32533/CVE-2022-33980/CVE-2021-37839)
Ml self realization / logistic regression / binary classification
Nmap tool introduction and common commands
leetcode 865. Smallest Subtree with all the Deepest Nodes | 865.具有所有最深节点的最小子树(树的BFS,parent反向索引map)
Why did MySQL query not go to the index? This article will give you a comprehensive analysis
Installing and using mpi4py
[target tracking] |atom
Cross modal semantic association alignment retrieval - image text matching
Redission源码解析
给刚入门或者准备转行网络工程师的朋友一些建议
Tencent game client development interview (unity + cocos) double bombing social recruitment 6 rounds of interviews
How mysql/mariadb generates core files
Why does the updated DNS record not take effect?
云原生应用开发之 gRPC 入门
Optimization of ecological | Lake Warehouse Integration: gbase 8A MPP + xeos
WPF custom realistic wind radar chart control
Introduction to ADB tools