当前位置:网站首页>[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
1
1
1
1
1
1
0
1
n
n
n-1
1
1
n&(n-1)
n
n=0
1
Source 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 & 1
n & (n-1)
边栏推荐
- Looking back at the moon
- 卷积神经网络(CNN)详解及TensorFlow2代码实现
- 大家都能看得懂的源码(一)ahooks 整体架构篇
- 十大券商注册开户有没有什么风险?安全吗?
- 会计要素包括哪些内容
- 网络爬虫2:抓取网易云音乐评论用户ID及主页地址
- Cause analysis of 12 MySQL slow queries
- Many gravel 3D material mapping materials can be obtained with one click
- [serial] shuotou O & M monitoring system 01 overview of monitoring system
- 第2章 构建自定义语料库
猜你喜欢
基于SSH框架的学生信息管理系统
Gee: calculate the maximum and minimum values of pixels in the image area
[Bayesian classification 2] naive Bayesian classifier
In 2022, where will the medium and light-weight games go?
leetcode刷题:字符串02( 反转字符串II)
The importance of using fonts correctly in DataWindow
GEE:计算image区域内像素最大最小值
About appium trample pit: encountered internal error running command: error: cannot verify the signature of (solved)
VB.net类库,获取屏幕内鼠标下的颜色(进阶——3)
【贝叶斯分类2】朴素贝叶斯分类器
随机推荐
如何在 SAP BTP 平台上启用 HANA Cloud 服务
后台查找,如何查找网站后台
[serial] shuotou O & M monitoring system 01 overview of monitoring system
How to enable Hana cloud service on SAP BTP platform
JWT operation tool class sharing
leetcode刷题:字符串03(剑指 Offer 05. 替换空格)
Chapter 2 construction of self defined corpus
The postgraduate entrance examination in these areas is crazy! Which area has the largest number of candidates?
【连载】说透运维监控系统01-监控系统概述
KDD2022 | 基于知识增强提示学习的统一会话推荐系统
Which securities company is the most convenient, safe and reliable for opening an account
Student information management system based on SSH Framework
记录一次Redis大Key的排查
VB.net类库(进阶版——1)
Background search, how to find the website background
Many gravel 3D material mapping materials can be obtained with one click
[Bayesian classification 2] naive Bayesian classifier
Twenty five of offer - all paths with a certain value in the binary tree
Arrête d'être un bébé géant.
Configure redis master-slave and sentinel sentinel in the centos7 environment (solve the problem that the sentinel does not switch when the master hangs up in the ECS)