当前位置:网站首页>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;
}
}

边栏推荐
- Writing skills of multi plate rotation strategy -- strategy writing learning materials
- Some information about the developer environment in Chengdu
- FFMpeg filter
- Classes in TS
- [set theory] set concept and relationship (true subset | empty set | complete set | power set | number of set elements | power set steps)
- Know that Chuangyu cloud monitoring - scanv Max update: Ecology OA unauthorized server request forgery and other two vulnerabilities can be detected
- Asp access teaching management system design finished product
- Drf--- quick start 01
- C language series - Section 3 - functions
- Database management tool, querious direct download
猜你喜欢

When using the benchmarksql tool to test the concurrency of kingbasees, there are sub threads that are not closed in time after the main process is killed successfully

Golang -- realize file transfer

2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills

Youdao cloud notes

Prefix and (continuously updated)

Contents of welder (primary) examination and welder (primary) examination in 2022

Auman Galaxy new year of the tiger appreciation meeting was held in Beijing - won the double certification of "intelligent safety" and "efficient performance" of China Automotive Research Institute

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

540. Single element in ordered array

Preliminary cognition of C language pointer
随机推荐
P35-P41 fourth_ context
Know that Chuangyu cloud monitoring - scanv Max update: Ecology OA unauthorized server request forgery and other two vulnerabilities can be detected
[文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN
[literature reading] sparse in deep learning: practicing and growth for effective information and training in NN
[PCL self study: filtering] introduction and use of various filters in PCL (continuously updated)
How to choose cross-border e-commerce multi merchant system
Preliminary cognition of C language pointer
The latest activation free version of Omni toolbox
When using the benchmarksql tool to preheat data for kingbasees, execute: select sys_ Prewarm ('ndx_oorder_2 ') error
Employee attendance management system based on SSM
Basic use of continuous integration server Jenkins
FuncS sh file not found when using the benchmarksql tool to test kingbases
xrandr修改分辨率與刷新率
Joint set search: merge intervals and ask whether two numbers are in the same set
Contents of welder (primary) examination and welder (primary) examination in 2022
Introduction of pointer variables in function parameters
Mount NFS in kubesphere
消息队列(MQ)介绍
[set theory] set concept and relationship (true subset | empty set | complete set | power set | number of set elements | power set steps)
Why should programmers learn microservice architecture if they want to enter a large factory?