当前位置:网站首页>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;
}
}
边栏推荐
- Introduction to RFM analysis
- CTFSHOW 常用姿势篇(821-830)
- Intervention analysis + pseudo regression
- 1-72 convert string to decimal integer
- Detailed explanation of KOA development process
- Introduction and use of ES6
- 第007天:go语言字符串
- H5 the blue background color appears when clicking the picture
- Express scaffold creation
- EIA map making - data processing + map making
猜你喜欢
Common terms of electromagnetic compatibility
高等数学(第七版)同济大学 习题1-3 个人解答
电磁兼容常用名词术语
Analysis of the implementation principle of an open source markdown to rich text editor
Catalan number
[notes] summarize common horizontal and vertical centering methods
力扣刷题338.比特位计数
Small program input element moving up
Common encryption and decryption function encapsulation - AES encryption and decryption
Lightweight digital mall system based on PHP
随机推荐
Small program input element moving up
7-289 tag count (300 points)
Get parameters on link
Answer private message @ Tiantian Wx //2022-6-12 C language 51 single chip microcomputer led analog traffic light
SEO specification
CTFSHOW 常用姿势篇(821-830)
VGA display based on de2-115 platform
Redis data persistence
Billions of data to determine whether the element exists
Solution to failure to download files by wechat scanning QR code
The problem that ionic3 cannot automatically install the APK package
Introduction and use of ES6
【LeetCode】860. Change with lemonade (2 brushes for wrong questions)
Principle and control program of single chip microcomputer serial port communication
在线音频调节技术汇总
Analysis of the implementation principle of an open source markdown to rich text editor
SQL 进阶挑战(1 - 5)
PAT 1054 The Dominant Color
Uni app dynamic add style dynamic bind background image invalid
JSTL -- JSP standard tag library