当前位置:网站首页>Li Kou daily question - day 30 -1523 Number of bit 1
Li Kou daily question - day 30 -1523 Number of bit 1
2022-06-29 06:37:00 【Chongyou research Sen】
2022.6.28 Did you brush the questions today ?
subject :
Write a function , Input is an unsigned integer ( In the form of a binary string ), Returns the number of digits in its binary expression as '1' The number of ( Also known as Hamming weight ).
analysis :
To give you one 32 Bit integers , for example 01, You need to find out how many there are 1, Then return the number
Ideas : We make use of n&n-1 The formula , By changing the formula, the lowest bit of each time can be achieved 1 Turn into 0. So we put all 1 Turn into 0, Count the number of changes , Namely 1 The number of .
analysis :
class Solution {
public:
int hammingWeight(uint32_t n) {
int ret = 0;
while (n) {
n &= n - 1;
ret++;
}
return ret;
}
};
边栏推荐
- What should I learn before learning programming?
- 多线程工具类 CompletableFuture
- Servlet version conflict causes page 404
- [high concurrency] deeply analyze the callable interface
- Are there too many programmers in China at present?
- Haar cascades and LBP cascades in face detection [closed] - Haar cascades vs. LBP cascades in face detection [closed]
- Fault: NetBt log for id4321
- Antd work item memo w3.0
- Fault: KDC warning log for id29
- Rich material libraries make modeling easy and efficient for developers
猜你喜欢
![[deep learning] - maze task learning I (to realize the random movement of agents)](/img/c1/95b476ec62436a35d418754e4b11dc.jpg)
[deep learning] - maze task learning I (to realize the random movement of agents)

Design and practice of kubernetes cluster and application monitoring scheme

Fresnel diffraction with rectangular aperture based on MATLAB

What is the "danksharding" of V God Kop on Valentine's day?

Hyperledger Fabric 2. X custom smart contract

Servlet version conflict causes page 404

关于DDNS

Delete tag

Servlet version conflict causes page 404

Pytest (7) -yield and termination function
随机推荐
Pytest (7) -yield and termination function
Design of leetcode simple problem goal parser
After “Go to Definition”, is there a command to return to where you came from?
QT (x): innosetup for software packaging
Part 63 - interpreter and compiler adaptation (II)
Benign competition will promote each other
Segment in Lucene
2022.02.15 - SX10-31. House raiding III
The echares map is implemented separately by provinces, and the tooltip user-defined prompt box, scattered annotation and scattered illumination are explained in detail
'only_ full_ group_ The influence of by'sql mode on group by and its treatment
RedisTemplate处理hash整数类型的问题解析
Will the order of where conditions in MySQL affect the union index? Will where 1 =1 affect the use of the index? Does where 1 =1 affect the use of indexes?
Easy to understand TCP four waves (multi picture explanation)
2022.02.14 - 239. A single element in an ordered array
I would like to ask what securities dealers recommend? Is it safe to open an account online?
[c language] [sword finger offer article] - print linked list from end to end
关于 localStorage 的一些高阶用法
JIRA basic usage sharing
Agile invincible event
Servlet version conflict causes page 404