当前位置:网站首页>Sword finger offer 15 Number of 1 in binary
Sword finger offer 15 Number of 1 in binary
2022-07-03 12:59:00 【Hiccup~~~~】
The finger of the sword Offer 15. Binary 1 The number of
The difficulty is simple 251
Write a function , Input is an unsigned integer ( In the form of a binary string ), Returns the number of digits in its binary expression as ‘1’ The number of ( Also known as Hamming weight ).).
Tips :
- 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 Example 3 in , The input represents a signed integer -3.
Example 1:
Input :n = 11 ( Console input 00000000000000000000000000001011) Output :3 explain : Binary string of input
00000000000000000000000000001011 in , There are three of them '1'.
Example 2:
Input :n = 128 ( Console input 00000000000000000000000010000000) Output :1 explain : Binary string of input 00000000000000000000000010000000 in , There is one in all for '1'.
Example 3:
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'.
Tips :
- The input must be of length 32 Of Binary string .
Code
class Solution {
public:
int hammingWeight(uint32_t n) {
int flag=0;
while(n){
if(n&1==1)
flag++;
n=n>>1;
}
return flag;
}
};
边栏推荐
- GaN图腾柱无桥 Boost PFC(单相)七-PFC占空比前馈
- [data mining review questions]
- 【Colab】【使用外部数据的7种方法】
- ncnn神经网络计算框架在香橙派OrangePi 3 LTS开发板中的使用介绍
- mysqlbetween实现选取介于两个值之间的数据范围
- C graphical tutorial (Fourth Edition)_ Chapter 20 asynchronous programming: examples - using asynchronous
- Openstack node address change
- Kung Fu pays off, and learning is done
- Solve the problem of VI opening files with ^m at the end
- Low code platform international multilingual (I18N) technical solution
猜你喜欢

阿里 & 蚂蚁自研 IDE

(latest version) WiFi distribution multi format + installation framework

How to convert a decimal number to binary in swift

【数据库原理及应用教程(第4版|微课版)陈志泊】【第六章习题】

studio All flavors must now belong to a named flavor dimension. Learn more

Solve the problem of VI opening files with ^m at the end

(最新版) Wifi分销多开版+安装框架

高效能人士的七个习惯

Xctf mobile--app2 problem solving

Idea full text search shortcut ctr+shift+f failure problem
随机推荐
Social community forum app ultra-high appearance UI interface
自抗扰控制器七-二阶 LADRC-PLL 结构设计
2022-02-11 practice of using freetsdb to build an influxdb cluster
剑指 Offer 12. 矩阵中的路径
Glide question you cannot start a load for a destroyed activity
[exercise 6] [Database Principle]
(最新版) Wifi分销多开版+安装框架
Low code platform international multilingual (I18N) technical solution
CNN MNIST handwriting recognition
sitesCMS v3.0.2发布,升级JFinal等依赖
2022-01-27 research on the minimum number of redis partitions
4. 无线体内纳米网:电磁传播模型和传感器部署要点
Quick learning 1.8 front and rear interfaces
如何在微信小程序中获取用户位置?
【数据库原理及应用教程(第4版|微课版)陈志泊】【第三章习题】
剑指 Offer 16. 数值的整数次方
Kotlin - 改良装饰者模式
IDEA 全文搜索快捷键Ctr+Shift+F失效问题
【R】【密度聚类、层次聚类、期望最大化聚类】
Kotlin notes - popular knowledge points asterisk (*)