当前位置:网站首页>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;
}
}
边栏推荐
- Interpretation and implementation of proxy mode
- Real time requirements for 5g China Unicom repeater network management protocol
- Idea Download
- MCU: NEC protocol infrared remote controller
- Analyse du principe de mise en œuvre d'un éditeur de texte open source markdown - to - rich
- R: Airline customer value analysis practice
- Et framework -22 creating serverinfo entities and events
- CTFSHOW SQL注入篇(231-253)
- Real time question answering of single chip microcomputer / embedded system
- MySQL index
猜你喜欢
The could not find com scwang. smart:refresh-layout-kernel:2.0.3. Required by: project: the app cannot load the third-party package
Mongodb compass connects to the Alibaba cloud remote server database or reports an error occurred while loading instance info: command hostinfo req
Unity shader learning 004 shader debugging platform difference third-party debugging tools
Lightweight digital mall system based on PHP
SCM: introduction to Modbus communication protocol
MCU: RS485 communication and Modbus Protocol
knife4j aggregation 2.0.9支持路由文档自动刷新
Single chip microcomputer: pcf8591 application program
Introduction and use of ES6
EMC整改纲要
随机推荐
出现Could not find com.scwang.smart:refresh-layout-kernel:2.0.3.Required by: project :app 无法加载第三方包情况
R: Airline customer value analysis practice
Application of dagger2 learning module (II)
Filter and listener
Notes on uni app
R: Employee turnover forecast practice
Redis
Ultra quicksort reverse sequence pair
Configuration and practice of shardingsphere JDBC sub database separation of read and write
[kubernetes series] pod chapter actual operation
第007天:go语言字符串
Ladder race
十億數據量 判斷元素是否存在
PAT 1054 The Dominant Color
Single chip microcomputer: d/a output
MCU: pcf8591 hardware interface
Zoom and move the H5 part of the mobile end
dumi 搭建文档型博客
Introduction and use of ES6
Differences and relations between three-tier architecture and MVC