当前位置:网站首页>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;
}
}
边栏推荐
- RSRS指标择时及大小盘轮动
- 一名外包仔的2022年中总结
- [Chongqing Guangdong education] reference materials for design and a better life of Zhongyuan Institute of science and technology
- [pat (basic level) practice] - [simple simulation] 1063 calculate the spectral radius
- Factor stock selection scoring model
- BMZCTF simple_ pop
- [set theory] binary relationship (special relationship type | empty relationship | identity relationship | global relationship | divisive relationship | size relationship)
- 使用BENCHMARKSQL工具对KingbaseES预热数据时执行:select sys_prewarm(‘NDX_OORDER_2 ‘)报错
- Arthas watch grabs a field / attribute of the input parameter
- Dismantle a 100000 yuan BYD "Yuan". Come and see what components are in it.
猜你喜欢
Which code editor is easy to use? Code editing software recommendation
2022 registration of G2 utility boiler stoker examination and G2 utility boiler stoker reexamination examination
Redis persistence principle
Basic use of continuous integration server Jenkins
FuncS sh file not found when using the benchmarksql tool to test kingbases
有道云笔记
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
Human resource management system based on JSP
Two points -leetcode-540 A single element in an ordered array
Library management system based on SSM
随机推荐
[set theory] set identities (idempotent law | exchange law | combination law | distribution rate | De Morgan law | absorption rate | zero law | identity | exclusion law | contradiction law | complemen
2022-02-12 (338. Bit count)
vulnhub HA: Natraj
RSRS指标择时及大小盘轮动
Design and implementation of JSP logistics center storage information management system
2022-02-13 (347. Top k high frequency elements)
Xrandr modify resolution and refresh rate
[BMZCTF-pwn] 20-secret_ file
Joint set search: merge intervals and ask whether two numbers are in the same set
Jincang KFS data bidirectional synchronization scenario deployment
Why should programmers learn microservice architecture if they want to enter a large factory?
Use the benchmarksql tool to perform a data prompt on kingbases. The jdbc driver cannot be found
4 years of experience to interview test development, 10 minutes to end, ask too
Interface in TS
220214c language learning diary
[set theory] binary relationship (definition field | value field | inverse operation | inverse synthesis operation | restriction | image | single root | single value | nature of synthesis operation)
PostgreSQL database high availability Patroni source code learning - etcd class
[文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN
What are the Bluetooth headsets with good sound quality in 2022? Inventory of four high-quality Bluetooth headsets
Contents of welder (primary) examination and welder (primary) examination in 2022