当前位置:网站首页>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;
}
}
边栏推荐
- 【测试平台开发】二十、完成编辑页发送接口请求功能
- Educational Codeforces Round 132 (Rated for Div. 2) D. Rorororobot
- OpenCV计算外包矩形
- C# 线程锁和单多线程简单使用
- 粒子边界碰撞的处理
- 【测试平台开发】23. 接口断言功能-保存接口断言和编辑回显
- mysql函数汇总之字符串函数
- Can bus quick understanding
- Postgresql快照优化Globalvis新体系分析(性能大幅增强)
- Russia hopes to effectively implement the "package" agreement on the export of agricultural products
猜你喜欢

直播课堂系统02-搭建项目环境
![[applet automation minium] i. framework introduction and environment construction](/img/1f/95b78e6574c3af3ff7abcf5db838f5.png)
[applet automation minium] i. framework introduction and environment construction

Postgresql快照优化Globalvis新体系分析(性能大幅增强)

初识C语言函数

C language project practice: 24 point game calculator (based on knowledge points such as structure, pointer, function, array, loop, etc.)

C thread lock and single multithreading are simple to use
It is suggested that Siyuan notes can be compatible with third-party sync disks

什麼是Per-Title編碼?

【测试平台开发】十七、接口编辑页面实现下拉级联选择,绑定接口所属模块...

面试官:生成订单30分钟未支付,则自动取消,该怎么实现?
随机推荐
@Feignclient detailed tutorial (illustration)
Postgresql快照优化Globalvis新体系分析(性能大幅增强)
numpy和pytorch的版本对应关系
[software test] MQ abnormal test encountered in disk-to-disk work
Argocd user management, RBAC control, script login, APP synchronization
Leetcode: 17. letter combination of phone number
什么是Per-Title编码?
NVIDIA vid2vid paper reproduction
Fastapi application joins Nacos
dataframe.groupby学习资料
mysql函数汇总之数学函数
AVX指令集加速矩阵乘法
【无标题】
@FeignClient使用详细教程(图解)
Program design of dot matrix Chinese character display of basic 51 single chip microcomputer
数字相加的精度问题
[转]基于POI的功能区划分()
CSDN写文方法(二)
C# 线程锁和单多线程简单使用
mysql 之general_log日志