当前位置:网站首页>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;
}
};
边栏推荐
- 2022-01-27 redis cluster brain crack problem analysis
- 【数据库原理及应用教程(第4版|微课版)陈志泊】【第四章习题】
- Xctf mobile--rememberother problem solving
- [combinatorics] permutation and combination (the combination number of multiple sets | the repetition of all elements is greater than the combination number | the derivation of the combination number
- Integer case study of packaging
- How to get user location in wechat applet?
- (最新版) Wifi分销多开版+安装框架
- Detailed explanation of the most complete constraintlayout in history
- 基于同步坐标变换的谐波电流检测
- 【习题五】【数据库原理】
猜你喜欢

并网-低电压穿越与孤岛并存分析

Xctf mobile--app3 problem solving

Analysis of a music player Login Protocol

4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment

Node. Js: use of express + MySQL

GaN图腾柱无桥 Boost PFC(单相)七-PFC占空比前馈

Differences between initial, inherit, unset, revert and all

最新版盲盒商城thinkphp+uniapp

OpenHarmony应用开发之ETS开发方式中的Image组件

低代码平台国际化多语言(i18n)技术方案
随机推荐
Glide question you cannot start a load for a destroyed activity
Glide 4.6.1 API initial
Kotlin - 改良装饰者模式
(最新版) Wifi分销多开版+安装框架
Export the entire Oracle Database
4. 无线体内纳米网:电磁传播模型和传感器部署要点
【习题七】【数据库原理】
[review questions of database principles]
SSH登录服务器发送提醒
Project video based on Linu development
Ten workplace rules
强大的头像制作神器微信小程序
C graphical tutorial (Fourth Edition)_ Chapter 13 entrustment: delegatesamplep245
context. Getexternalfilesdir() is compared with the returned path
ImportError: No module named examples. tutorials. mnist
2022-02-11 practice of using freetsdb to build an influxdb cluster
SQL learning notes (I)
Kung Fu pays off, and learning is done
Xctf mobile--app1 problem solving
Method overloading and rewriting