当前位置:网站首页>[brush through sword finger] sword finger offer II 003 Number of 1 in the first n digit binary
[brush through sword finger] sword finger offer II 003 Number of 1 in the first n digit binary
2022-06-09 01:23:00 【Wind fall_】
Integer article - The finger of the sword Offer II 003. front n A number in binary 1 The number of
Original link : The finger of the sword Offer II 003. front n A number in binary 1 The number of
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-t8c8NYxf-1654664972724)(https://cdn.jsdelivr.net/gh/FengLuo97/imgurl/img202206081257747.png)]](/img/4f/695bc08f4d2d7a2a33f6b13ebb40a8.png)
analysis
Simple solution : By binary operation , Every time you move right 1 position , And bitwise pair 1 The sum of this number can be calculated 1 The number of
Advanced solution : adopt i & (i-1) seek 1 The number of . Consider a binary number i = 1100, be i-1 = 1011, that i & (i-1) = 1000, At this point, we find a value which is 1 Bit , Add one counter , Cycle until i by 0, The number of cycles is the binary of that number 1 The number of .
For the naive solution , The time complexity is O(n*k),k Is the number of bits in binary ;
For advanced solution , The time complexity is O(n*m),m It is... In binary 1 Number of digits ;
Code
Simple solution
class Solution {
public int[] countBits(int n) {
int[] ans = new int[n + 1];
for (int i = 0; i <= n; i++) {
int cnt_One = 0;
int temp = i;
while (temp != 0) {
if ((temp & 1) == 1) {
cnt_One++;
}
temp = temp >> 1;
}
ans[i] = cnt_One;
}
return ans;
}
}

Advanced solution
class Solution {
public int[] countBits(int n) {
int[] ans = new int[n + 1];
for (int i = 0; i <= n; i++) {
int cnt_One = 0;
int temp = i;
while (temp != 0) {
temp = temp & (temp - 1);
cnt_One++;
}
ans[i] = cnt_One;
}
return ans;
}
}

Writing is not easy , Ask for attention , No attention to , Just like it !![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-lyMxWZoP-1654664972726)(https://cdn.jsdelivr.net/gh/FengLuo97/imgurl/img202206081257357.gif)]](https://img-blog.csdnimg.cn/fa408696615d4001ae68c77290ce8aac.gif)
边栏推荐
- Laravel基于MongoDB(LBS)实现附近的人、店铺、房源
- Web slider drag selection value slider plug-in
- A collection of commonly used SQL scripts is recommended!
- jenkins根据凭证ID查看忘记的凭证密码以及重置admin密码的操作方法
- shell 大写转小写
- intel 加速雲數智變革
- 汇编语言集成开发环境学习笔记
- Shell disk space usage
- Shell simple interaction
- From just entering the testing industry to doubling my salary: talk about my advanced testing experience, which is worth learning from
猜你喜欢

Take a data catalog architecture upgrade as an example to talk about the performance optimization of the business system

数论 --- 朴素筛法、埃氏筛法、线性筛法

2022dasctf APR x fat epidemic prevention challenge warmup PHP

谷歌chrome利用Bookmarks文件找回书签

Cobalt strike certificate modification bypasses traffic review

汇编语言集成开发环境学习笔记

jenkins根据凭证ID查看忘记的凭证密码以及重置admin密码的操作方法

Analyse du principe d'optimisation d'une requête SQL: 900W + données, de 17s à 300ms
Docker installation redis configuration remote connection and stepping on pits

Flat login form page
随机推荐
並發性能測試的種類與指標
音视频的理论基础与底层技术研究资料
Shell 报告服务器信息
It took me two days to make a somatosensory game console
Matrix - 2D area and retrieval - matrix immutable, leetcode 304
[JS] pseudo array to array
Shell color output
shell 大写转小写
2022-06-08: find the shortest sub array with "maximum or result" in the non negative array and return the shortest length.
Hqchart tutorial 30- how to connect the K-line diagram with the third-party data 35- fixed range trading volume distribution diagram data
涉案资金超10亿,又一洗钱团伙被端,“二清”警钟不能忘
intel 加速雲數智變革
iscc-2022
Lancher single node deployment and certificate problem -- the way to build a dream
Today's sleep quality record 76 points
自定义分页
[tgcalls] managers who track and debug calls
Notes on supporting geo data mining live broadcast of Shengxin skill tree
Shell 减法
shell 天气预报