当前位置:网站首页>【题解】剑指 Offer 15. 二进制中1的个数(C语言)
【题解】剑指 Offer 15. 二进制中1的个数(C语言)
2022-06-26 21:28:00 【InfoQ】
️剑指 Offer 15. 二进制中1的个数️
题目详情
- 请注意,在某些语言(如 Java)中,没有无符号整数类型。在这种情况下,输入和输出都将被指定为有符号整数类型,并且不应影响您的实现,因为无论整数是有符号的还是无符号的,其内部的二进制表示形式都是相同的。在 Java 中,编译器使用 二进制补码 记法来表示有符号整数。因此,在上面的 第三个示例 中,输入表示有符号整数 -3。
- 输入必须是长度为 32 的 二进制串 。
输入:n = 11 (控制台输入 00000000000000000000000000001011)
输出:3
解释:输入的二进制串 00000000000000000000000000001011 中,共有三位为 '1'。
输入:n = 128 (控制台输入 00000000000000000000000010000000)
输出:1
解释:输入的二进制串 00000000000000000000000010000000 中,共有一位为 '1'。
输入:n = 4294967293 (控制台输入 11111111111111111111111111111101,部分语言中 n = -3)
输出:31
解释:输入的二进制串 11111111111111111111111111111101 中,共有 31 位为 '1'。
无
解题思路
1
1
1
1
1
1
0
1
n
n
n-1
1
1
n&(n-1)
n
n=0
1
源代码
//方法1
int hammingWeight(uint32_t n) {
int cnt = 0;
while (n)
{
if (n & 1)
{
cnt++;
}
n >>= 1;
}
return cnt;
}

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

总结
n & 1
n & (n-1)
边栏推荐
- Comment installer la base de données MySQL 8.0 sous Windows? (tutoriel graphique)
- Shiniman household sprint A shares: annual revenue of nearly 1.2 billion red star Macalline and incredibly home are shareholders
- Mr. Sun's version of JDBC (21:34:25, June 12, 2022)
- [Bayesian classification 3] semi naive Bayesian classifier
- VB.net类库(进阶——2 重载)
- [Bayesian classification 4] Bayesian network
- [Shandong University] information sharing for the first and second examinations of postgraduate entrance examination
- Gee: calculate the maximum and minimum values of pixels in the image area
- 基于QT实现简单的连连看小游戏
- 指南针能开户炒股吗?安全吗?
猜你喜欢
众多碎石3d材质贴图素材一键即可获取
YuMinHong: New Oriental does not have a reversal of falling and turning over, destroying and rising again
Leetcode question brushing: String 01 (inverted string)
关于appium踩坑 :Encountered internal error running command: Error: Cannot verify the signature of (已解决)
The postgraduate entrance examination in these areas is crazy! Which area has the largest number of candidates?
The importance of using fonts correctly in DataWindow
[protobuf] some pits brought by protobuf upgrade
【protobuf 】protobuf 升级后带来的一些坑
Leetcode(763)——划分字母区间
基于QT开发的线性代数初学者的矩阵计算器设计
随机推荐
【贝叶斯分类4】贝叶斯网
0 basic C language (3)
The postgraduate entrance examination in these areas is crazy! Which area has the largest number of candidates?
Détails de l'annotation des ressources sentinelles
0基础学c语言(1)
俞敏洪:新东方并不存在倒下再翻身,摧毁又雄起的逆转
基于Qt实现的“合成大西瓜”小游戏
会计要素包括哪些内容
【连载】说透运维监控系统01-监控系统概述
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)
【贝叶斯分类2】朴素贝叶斯分类器
How to install mysql8.0 database under Windows system? (Graphic tutorial)
KDD2022 | 基于知识增强提示学习的统一会话推荐系统
VB.net类库——4给屏幕截图,裁剪
MATLAB与Mysql数据库连接并数据交换(基于ODBC)
The latest 2022 research review of "continuous learning, CL"
Arrête d'être un bébé géant.
Is it safe to open an online account in case of five-year exemption?
Leetcode: String 04 (reverse the words in the string)
Looking back at the moon