当前位置:网站首页>Li Kou brush question 338 Bit count
Li Kou brush question 338 Bit count
2022-06-13 04:26:00 【GRT has to keep working hard】
The idea is : Write a function to calculate binary bits , And then from 0~n Traverse in turn , Get the returned linked list .
Brian Kernighan The principle of the algorithm is : For any integer x, Make x=x & (x−1), The operation will x The last binary representation of 1 become 0. therefore , Yes x Repeat the operation , until x become 0.
class Solution {
public int[] countBits(int n) {
int[] arr = new int[n+1];
for(int i=0; i<=n; i++){
arr[i] = countOne(i);
}
return arr;
}
public int countOne(int x){
int res = 0;
while(x>0){
x=x&(x-1);
res++;
}
return res;
}
}
边栏推荐
- MCU: EEPROM multi byte read / write operation sequence
- H5 the blue background color appears when clicking the picture
- 120. 三角形最小路径和-动态规划
- knife4j aggregation 2.0.9支持路由文档自动刷新
- 【LeetCode】860. Change with lemonade (2 brushes for wrong questions)
- Applet version update
- Explanation of line segment tree
- JS common array methods
- dumi 搭建文檔型博客
- EMC rectification outline
猜你喜欢

How to use debounce in lodash to realize anti shake

Sword finger offer II 022 Entry node of a link in a linked list

干预分析 + 伪回归

Mongodb compass connects to the Alibaba cloud remote server database or reports an error occurred while loading instance info: command hostinfo req

R: Employee turnover forecast practice

Introduction to MCU peripherals: temperature sensor DS18B20

Single chip microcomputer: MODBUS multi computer communication program design

高等数学(第七版)同济大学 习题1-3 个人解答
![[kubernetes series] pod chapter actual operation](/img/ac/fb563f3d92e6fdbd36e41d2f213556.jpg)
[kubernetes series] pod chapter actual operation

1-72 convert string to decimal integer
随机推荐
Applet version update
Single chip microcomputer: main index of a/d (analog-to-digital conversion)
The could not find com scwang. smart:refresh-layout-kernel:2.0.3. Required by: project: the app cannot load the third-party package
Summary of webdriver API for web automated testing
Catalan number
【剑指Offer】面试题24.反转链表
Introduction to RFM analysis
Single chip microcomputer: a/d differential input signal
Differences and relations between three-tier architecture and MVC
SCM: introduction and operation of EEPROM
出现Could not find com.scwang.smart:refresh-layout-kernel:2.0.3.Required by: project :app 无法加载第三方包情况
Hugo blog building tutorial
Redis
MySQL索引
Understand the pseudo static configuration to solve the 404 problem of refreshing the page of the deployment project
SCM: introduction to Modbus communication protocol
Uni app dynamic add style dynamic bind background image invalid
Call C function in Lua
Explanation of line segment tree
10 minutes to thoroughly understand how to configure sub domain names to deploy multiple projects