当前位置:网站首页>[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)边栏推荐
- 0 basic C language (1)
- 财务费用分析怎么分析
- The importance of using fonts correctly in DataWindow
- windows系统下怎么安装mysql8.0数据库?(图文教程)
- Is there any risk in opening a mobile stock registration account? Is it safe?
- Usage of MGrid in numpy
- AI智能抠图工具--头发丝都可见
- 基于Qt实现的“合成大西瓜”小游戏
- numpy中mgrid的用法
- How to enable Hana cloud service on SAP BTP platform
猜你喜欢

Hands on deep learning pytorch version 3 - Data Preprocessing
![[protobuf] some pits brought by protobuf upgrade](/img/c1/01ba268c786c046d6fbc450a72ab45.png)
[protobuf] some pits brought by protobuf upgrade

Vi/vim editor

leetcode刷题:字符串05(剑指 Offer 58 - II. 左旋转字符串)

【protobuf 】protobuf 昇級後帶來的一些坑
![[Bayesian classification 3] semi naive Bayesian classifier](/img/9c/070638c1a613be648466e4f2bc341e.png)
[Bayesian classification 3] semi naive Bayesian classifier

MATLAB与Mysql数据库连接并数据交换(基于ODBC)

会计要素包括哪些内容

The importance of using fonts correctly in DataWindow

Yonghui released the data of Lantern Festival: the sales of Tangyuan increased significantly, and several people's livelihood products increased by more than 150%
随机推荐
Establish a connection with MySQL
龙芯中科科创板上市:市值357亿 成国产CPU第一股
API管理之利剑 -- Eolink
Netease Yunxin officially joined the smart hospital branch of China Medical Equipment Association to accelerate the construction of smart hospitals across the country
BN(Batch Normalization) 的理论理解以及在tf.keras中的实际应用和总结
证券注册开户有没有什么风险?安全吗?
0 basic C language (1)
Is it safe to open an online account in case of five-year exemption?
中金证券经理给的开户二维码办理股票开户安全吗?我想开个户
Cause analysis of 12 MySQL slow queries
基于SSH框架的学生信息管理系统
The latest 2022 research review of "continuous learning, CL"
如何在 SAP BTP 平台上启用 HANA Cloud 服务
Is there any risk in registering and opening an account for stock speculation? Is it safe?
C: 反转链表
股票炒股注册开户有没有什么风险?安全吗?
Many gravel 3D material mapping materials can be obtained with one click
SAP Commerce Cloud 项目 Spartacus 入门
【 protobuf 】 quelques puits causés par la mise à niveau de protobuf
SAP Spartacus 中的依赖注入 Dependency Injection 介绍