当前位置:网站首页>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;
}
}
边栏推荐
- 2022 new examination questions for the main principals of hazardous chemical business units and examination skills for the main principals of hazardous chemical business units
- Kingbasees plug-in KDB of Jincang database_ database_ link
- data2vec! New milestone of unified mode
- What are the Bluetooth headsets with good sound quality in 2022? Inventory of four high-quality Bluetooth headsets
- Busycal latest Chinese version
- AWS VPC
- [NLP]—sparse neural network最新工作简述
- GFS distributed file system (it's nice to meet it alone)
- [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
- How to use kotlin to improve productivity: kotlin tips
猜你喜欢
2022 registration of G2 utility boiler stoker examination and G2 utility boiler stoker reexamination examination
7. Integrated learning
Basic use of continuous integration server Jenkins
C language series - Section 3 - functions
vulnhub HA: Natraj
解决bp中文乱码
Feature_selection
Preliminary cognition of C language pointer
[fxcg] inflation differences will still lead to the differentiation of monetary policies in various countries
跨境电商多商户系统怎么选
随机推荐
PostgreSQL database high availability Patroni source code learning - etcd class
vulnhub HA: Natraj
2022 P cylinder filling test content and P cylinder filling simulation test questions
FuncS sh file not found when using the benchmarksql tool to test kingbases
使用BENCHMARKSQL工具对KingbaseES预热数据时执行:select sys_prewarm(‘NDX_OORDER_2 ‘)报错
Basic types of data in TS
Internationalization and localization, dark mode and dark mode in compose
P35-P41 fourth_ context
Dismantle a 100000 yuan BYD "Yuan". Come and see what components are in it.
因子选股-打分模型
有道云笔记
Interface in TS
Dive Into Deep Learning——2.1数据操作&&练习
Two points -leetcode-540 A single element in an ordered array
stm32逆向入门
Solve BP Chinese garbled code
X-ray normal based contour rendering
Library management system based on SSM
Ffmpeg mix
2022 Shandong Province safety officer C certificate examination content and Shandong Province safety officer C certificate examination questions and analysis