当前位置:网站首页>338. Bit count
338. Bit count
2022-07-23 14:58:00 【ATTACH_ Fine】
subject
Give you an integer n , about 0 <= i <= n Each of the i , Calculate its binary representation 1 The number of , Returns a length of n + 1 Array of ans As the answer .
Example :
Ideas
For all numbers , There are only two kinds of :
** Odd number :** In binary representation , The odd number must be one more than the even number before 1, Because more is the lowest 1.
give an example :
0 = 0 1 = 1
2 = 10 3 = 11
even numbers : In binary representation , In even numbers 1 The number of must be divided by 2 The number after that is the same . Because the lowest point is 0, Divide 2 Is to move one bit to the right , That is to put that 0 Just erase it , therefore 1 The number of is constant .
give an example :
2 = 10 4 = 100 8 = 1000
3 = 11 6 = 110 12 = 1100
initialization dp[0] = 0;
Code
class Solution {
public int[] countBits(int n) {
int[] res = new int[n+1];
res[0] = 0;
for(int i = 1; i <= n; i++){
if((i & 1) != 0) // Odd number
res[i] = res[i-1] + 1;
else
res[i] = res[i/2];
}
return res;
}
}
边栏推荐
- AVX指令集加速矩阵乘法
- Advanced operation and maintenance 02
- Qt文档阅读笔记-Audio Example解析
- Live classroom system 03 supplement model class and entity
- 爬虫中selenium实现自动给csdn博主文章点收藏
- 【无标题】
- Postgresql快照优化Globalvis新体系分析(性能大幅增强)
- 什么是Promise?Promise有什么好处
- dataframe.groupby学习资料
- Russia hopes to effectively implement the "package" agreement on the export of agricultural products
猜你喜欢

真人踩过的坑,告诉你避免自动化测试常犯的10个错误
![[test platform development] 23. interface assertion function - save interface assertion and edit echo](/img/36/aed4feb6a4e40b6b5c06206a8ed440.png)
[test platform development] 23. interface assertion function - save interface assertion and edit echo

21 - 二叉树的垂直遍历

【测试平台开发】二十、完成编辑页发送接口请求功能

精品国创《少年歌行》数字藏品开售,邀你共铸少年武侠江湖梦
![[applet automation minium] III. element positioning - use of wxss selector](/img/ec/51eadd08bea18f8292aa3521f11e8a.png)
[applet automation minium] III. element positioning - use of wxss selector

MySQL unique index has no duplicate value, and the error is repeated

爬虫中selenium实现自动给csdn博主文章点收藏

OpenHarmony南向学习笔记——Hi3861+HC-SR04超声波检测

supervisord安装使用
随机推荐
【软件测试】盘一盘工作中遇到的 MQ 异常测试
NVIDIA vid2vid paper reproduction
Kettle實現共享數據庫連接及插入更新組件實例
Redis | 非常重要的中间件
直播课堂系统01-数据库表设计
Prometheus入门使用(三)
cmake笔记
C# 线程锁和单多线程简单使用
Transferred from Yuxi information disclosure: products such as mRNA covid-19 vaccine and Jiuzhou horse tetanus immunoglobulin are expected to be on the market within this year.
mysql唯一索引无重复值报错重复
Zhongwang CAD professional 2022 software installation package download and installation tutorial
初识C语言函数
深度学习单图三维人脸重建
Building personal network disk based on nextcloud
[test platform development] 20. Complete the function of sending interface request on the edit page
Advanced operation and maintenance 03
Using shell script to block IP with high scanning frequency
Opencv calculation outsourcing rectangle
Openharmony South learning notes - hi3861+hc-sr04 ultrasonic testing
如何加速矩阵乘法——优化GEMM (CPU单线程篇)