当前位置:网站首页>[solution] sword finger offer 15 Number of 1 in binary (C language)
[solution] sword finger offer 15 Number of 1 in binary (C language)
2022-06-26 21:32:00 【InfoQ】
️ The finger of the sword Offer 15. Binary 1 The number of ️
Topic details
- Please note that , In some languages ( Such as Java) in , There is no unsigned integer type . under these circumstances , Both input and output will be specified as signed integer types , And should not affect your implementation , Because whether integers are signed or unsigned , Its internal binary representation is the same . stay Java in , The compiler uses Binary complement Notation to represent signed integers . therefore , Above The third example in , The input represents a signed integer -3.
- The input must be of length 32 Of Binary string .
Input :n = 11 ( Console input 00000000000000000000000000001011)
Output :3
explain : Binary string of input 00000000000000000000000000001011 in , There are three of them '1'.
Input :n = 128 ( Console input 00000000000000000000000010000000)
Output :1
explain : Binary string of input 00000000000000000000000010000000 in , There is one in all for '1'.
Input :n = 4294967293 ( Console input 11111111111111111111111111111101, In some languages n = -3)
Output :31
explain : Binary string of input 11111111111111111111111111111101 in , share 31 Position as '1'.
nothing
Their thinking
11111101nnn-111n&(n-1)nn=01Source code
// Method 1
int hammingWeight(uint32_t n) {
int cnt = 0;
while (n)
{
if (n & 1)
{
cnt++;
}
n >>= 1;
}
return cnt;
}

// Method 2
int hammingWeight(uint32_t n) {
int cnt = 0;
while (n)
{
n = n & (n - 1);
cnt++;
}
return cnt;
}

summary
n & 1n & (n-1)边栏推荐
- Many gravel 3D material mapping materials can be obtained with one click
- 12个MySQL慢查询的原因分析
- leetcode刷题:字符串01(反转字符串)
- leetcode刷题:哈希表08 (四数之和)
- 0 basic C language (1)
- Establish a connection with MySQL
- Redis + guava local cache API combination, performance burst!
- Leetcode: hash table 08 (sum of four numbers)
- Leetcode(452)——用最少数量的箭引爆气球
- Student information management system based on SSH Framework
猜你喜欢

第2章 构建自定义语料库

Leetcode question brushing: String 02 (reverse string II)

Leetcode question brushing: String 03 (Sword finger offer 05. replace space)

Kdd2022 𞓜 unified session recommendation system based on knowledge enhancement prompt learning

YuMinHong: New Oriental does not have a reversal of falling and turning over, destroying and rising again

龙芯中科科创板上市:市值357亿 成国产CPU第一股

基于启发式搜索的一字棋

【贝叶斯分类4】贝叶斯网

Gee: calculate the maximum and minimum values of pixels in the image area

分布式ID生成系统
随机推荐
Is there any risk in registering and opening an account for stock speculation? Is it safe?
基于SSH框架的学生信息管理系统
Icml2022 | neurotoxin: a lasting back door to federal learning
Android mediacodec hard coded H264 file (four), ByteDance Android interview
Listing of maolaiguang discipline on the Innovation Board: it is planned to raise 400million yuan. Fanyi and fanhao brothers are the actual controllers
The importance of using fonts correctly in DataWindow
leetcode刷题:字符串06(实现 strStr())
【连载】说透运维监控系统01-监控系统概述
Record a redis large key troubleshooting
API管理之利剑 -- Eolink
中金证券经理给的开户二维码办理股票开户安全吗?我想开个户
基于Qt实现的“合成大西瓜”小游戏
经典Wide & Deep模型介绍及tensorflow 2代码实现
[Bayesian classification 2] naive Bayesian classifier
网络爬虫终篇:向10万级网易云用户发送定向消息
基于启发式搜索的一字棋
Sword finger offer 12 Path in matrix
0 basic C language (1)
Godson China Science and technology innovation board is listed: the market value is 35.7 billion yuan, becoming the first share of domestic CPU
【贝叶斯分类4】贝叶斯网