当前位置:网站首页>Number of 1 in binary (simple difficulty)
Number of 1 in binary (simple difficulty)
2022-07-03 04:31:00 【Nulibiao】
Title Overview ( Medium difficulty )

Topic link :
Point me into the leetcode
Ideas and code
Train of thought display
The idea of this topic is still taken from us K God boss , I won't go into too much detail here
About logical shift right , Move left , If you are not clear about the right shift, you can see this solution :
Click to enter the solution
Or take a look at my blog :
Click me to enter the blog
Students who can't do binary subtraction can see this blog :
Click me to enter the blog
Code example
Code 1:
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int res = 0;
while(n != 0) {
res += n&1;
// Logical shift right
n >>>= 1;
}
return res;
}
}

Code 2:
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int res = 0;
while(n != 0) {
n &= (n-1);
res++;
}
return res;
}
}

边栏推荐
- [software testing-6] & Test Management
- C language series - Section 3 - functions
- 使用BENCHMARKSQL工具对kingbasees并发测试时kill掉主进程成功后存在子线程未及时关闭
- 540. Single element in ordered array
- Kingbasees plug-in KDB of Jincang database_ date_ function
- [文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN
- P35-P41 fourth_ context
- Triangular rasterization
- 2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills
- [fairseq] error: typeerror:_ broadcast_ coalesced(): incompatible function arguments
猜你喜欢
![[pat (basic level) practice] - [simple simulation] 1063 calculate the spectral radius](/img/01/c118725f74e39742df021b5dbcc33b.jpg)
[pat (basic level) practice] - [simple simulation] 1063 calculate the spectral radius

Use the benchmarksql tool to perform a data prompt on kingbases. The jdbc driver cannot be found

Basic use of continuous integration server Jenkins

Fcpx template: sweet memory electronic photo album photo display animation beautiful memory

BMZCTF simple_ pop

Youdao cloud notes

2022 Shandong Province safety officer C certificate examination content and Shandong Province safety officer C certificate examination questions and analysis

4 years of experience to interview test development, 10 minutes to end, ask too

FuncS sh file not found when using the benchmarksql tool to test kingbases

Data Lake three swordsmen -- comparative analysis of delta, Hudi and iceberg
随机推荐
Reptile exercise 03
Xrandr modify resolution and refresh rate
2022 Shandong Province safety officer C certificate examination content and Shandong Province safety officer C certificate examination questions and analysis
540. Single element in ordered array
[nlp] - brief introduction to the latest work of spark neural network
[set theory] binary relationship (definition field | value field | inverse operation | inverse synthesis operation | restriction | image | single root | single value | nature of synthesis operation)
会员积分商城系统的功能介绍
Redis persistence principle
Database management tool, querious direct download
[set theory] set concept and relationship (true subset | empty set | complete set | power set | number of set elements | power set steps)
Mongodb slow query optimization analysis strategy
C language series - Section 3 - functions
Redraw and reflow
After reviewing MySQL for a month, I was stunned when the interviewer of Alibaba asked me
[文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN
Pyqt control part (II)
2022 registration of G2 utility boiler stoker examination and G2 utility boiler stoker reexamination examination
4 years of experience to interview test development, 10 minutes to end, ask too
Reptile exercise 02
Human resource management system based on JSP