当前位置:网站首页>【题解】剑指 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'。
无
解题思路
11111101nnn-111n&(n-1)nn=01源代码
//方法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 & 1n & (n-1)边栏推荐
- Talk about my remote work experience | community essay solicitation
- 网易云信正式加入中国医学装备协会智慧医院分会,为全国智慧医院建设加速...
- 孙老师版本JDBC(2022年6月12日21:34:25)
- 0 basic C language (1)
- c语言99乘法表
- Godson China Science and technology innovation board is listed: the market value is 35.7 billion yuan, becoming the first share of domestic CPU
- leetcode刷题:字符串01(反转字符串)
- Redis + Guava 本地缓存 API 组合,性能炸裂!
- SAP Spartacus 默认路由配置的工作原理
- 后台查找,如何查找网站后台
猜你喜欢
![[Shandong University] information sharing for the first and second examinations of postgraduate entrance examination](/img/f9/68b5b5ce21f4f851439fa061b477c9.jpg)
[Shandong University] information sharing for the first and second examinations of postgraduate entrance examination

这些地区考研太疯狂!哪个地区报考人数最多?

Yonghui released the data of Lantern Festival: the sales of Tangyuan increased significantly, and several people's livelihood products increased by more than 150%

Record a redis large key troubleshooting

第2章 构建自定义语料库

传纸条【动态规划】

Background search, how to find the website background

Vi/vim editor
![[Bayesian classification 2] naive Bayesian classifier](/img/44/dbff297e536508a7c18b76b21db90a.png)
[Bayesian classification 2] naive Bayesian classifier

C language 99 multiplication table
随机推荐
What are the accounting elements
12个MySQL慢查询的原因分析
Is there any risk in registering and opening an account for stock speculation? Is it safe?
Y48. Chapter III kubernetes from introduction to mastery -- pod status and probe (21)
PostgreSQL notes
DAST 黑盒漏洞扫描器 第五篇:漏洞扫描引擎与服务能力
VB.net类库——4给屏幕截图,裁剪
DLA模型(分类模型+改进版分割模型) + 可变形卷积
会计要素包括哪些内容
Détails de l'annotation des ressources sentinelles
0 basic C language (3)
About appium trample pit: encountered internal error running command: error: cannot verify the signature of (solved)
关于appium踩坑 :Encountered internal error running command: Error: Cannot verify the signature of (已解决)
leetcode刷题:字符串04(颠倒字符串中的单词)
lotus configurations
The source code that everyone can understand (I) the overall architecture of ahooks
诗尼曼家居冲刺A股:年营收近12亿 红星美凯龙与居然之家是股东
「连续学习Continual learning, CL」最新2022研究综述
VB.net类库(进阶版——1)
【protobuf 】protobuf 昇級後帶來的一些坑