当前位置:网站首页>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;
}
}
边栏推荐
- Message scrolling JS implementation
- CTFSHOW SQL注入篇(211-230)
- Interpretation and implementation of proxy mode
- 重读经典:《End-to-End Object Detection with Transformers》
- 建模雜談系列143 數據處理、分析與决策系統開發的梳理
- dumi 搭建文档型博客
- ACM ICPC
- Summary of webdriver API for web automated testing
- Single chip microcomputer: d/a output
- Modeling discussion series 143 data processing, analysis and decision system development
猜你喜欢

Common terms of electromagnetic compatibility

VGA display based on de2-115 platform

Single chip microcomputer: pcf8591 application program

Differences and relations between three-tier architecture and MVC

Hugo blog building tutorial

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

Redis

Unity shader learning 004 shader debugging platform difference third-party debugging tools

R: Airline customer value analysis practice

Principle, composition and functions of sensors of Dajiang UAV flight control system
随机推荐
Configuration and practice of shardingsphere JDBC sub database separation of read and write
Express scaffold creation
MySQL index
Application of dagger2 learning module (II)
Billions of data to determine whether the element exists
Summary of webdriver API for web automated testing
El expression
Uni app enables pull-up loading and pull-down refresh (pull-down with animation)
十亿数据量 判断元素是否存在
knife4j aggregation 2.0.9支持路由文档自动刷新
120. triangle minimum path sum - Dynamic Planning
Understand the pseudo static configuration to solve the 404 problem of refreshing the page of the deployment project
在线音频调节技术汇总
Principle, composition and functions of sensors of Dajiang UAV flight control system
CTFSHOW 常用姿势篇(821-830)
R: Airline customer value analysis practice
Redis
MCU: RS485 communication and Modbus Protocol
干预分析 + 伪回归
Advanced Mathematics (Seventh Edition) Tongji University exercises 1-3 personal solutions