当前位置:网站首页>力扣每日一题-第30天-1523.位1的个数
力扣每日一题-第30天-1523.位1的个数
2022-06-29 06:32:00 【重邮研究森】
2022.6.28今天你刷题了吗?
题目:
编写一个函数,输入是一个无符号整数(以二进制串的形式),返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量)。
分析:
给你一个32位整型数,例如 01,你需要找到里面有多少个1,然后返回个数
思路:我们利用 n&n-1公式,改公式可以实现每次最低位的1变为0.所以我们把所有1变为0,统计变的次数,就是1的个数。
解析:
class Solution {
public:
int hammingWeight(uint32_t n) {
int ret = 0;
while (n) {
n &= n - 1;
ret++;
}
return ret;
}
};
边栏推荐
- 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?
- After “Go to Definition”, is there a command to return to where you came from?
- Sourcetree remote red exclamation point
- I would like to ask what securities dealers recommend? Is it safe to open an account online?
- Haar cascades and LBP cascades in face detection [closed] - Haar cascades vs. LBP cascades in face detection [closed]
- Stack -- 739 Daily temperature
- How to insert pseudo code into word documents simply and quickly?
- Design risc-v processor from scratch -- data adventure of five stage pipeline
- Rich material libraries make modeling easy and efficient for developers
- What should I learn before learning programming?
猜你喜欢

How to insert pseudo code into word documents simply and quickly?

Design of leetcode simple problem goal parser

The generation of leetcode simple questions each character is an odd number of strings

QT (x): innosetup for software packaging

Modularization and modular specification commonjs

Hyperledger Fabric 2. X custom smart contract

Pytest (7) -yield and termination function

Manual (functional) test 1

JIRA basic usage sharing

Sourcetree remote red exclamation point
随机推荐
ES6 Modularization: export /import
What is MES? What does it do?
Use of sed in shell script
2022-01 Microsoft vulnerability notification
Games101 Lecture 10 geometry 1 Notes
Rich material libraries make modeling easy and efficient for developers
Fault: display Storport driver out of date in component health
Failure: unable to log in to "taxpayer equity platform"
Mongodb sort function
Meso tetra (4-N, N, n-trimethylaminophenyl) porphyrin (ttmapp) /meso tetra - [4- (BOC threonine) aminophenyl] porphyrin (TAPP thr BOC) supplied by Qiyue
Stack -- 739 Daily temperature
Why are keys unordered in golang map
證券開戶安全麼,有沒有什麼危險呢
2022.02.15 - SX10-31. House raiding III
Fault: NetBt log for id4321
Installing modules in pycharm
The win11 file resource manager has an explicit Caton, and Microsoft promises to improve the performance in 2022
Design risc-v processor from scratch -- data adventure of five stage pipeline
Problems with MySQL database query
[high concurrency] deeply analyze the callable interface