当前位置:网站首页>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;
}
};
边栏推荐
- Swift return type is a function of function
- 【習題五】【數據庫原理】
- Nodejs+express+mysql realizes login function (including verification code)
- Redhat5 installing socket5 proxy server
- Everything comes to him who waits
- context. Getexternalfilesdir() is compared with the returned path
- C graphical tutorial (Fourth Edition)_ Chapter 13 entrustment: delegatesamplep245
- C graphical tutorial (Fourth Edition)_ Chapter 17 generic: genericsamplep315
- 并网-低电压穿越与孤岛并存分析
- Exploration of sqoop1.4.4 native incremental import feature
猜你喜欢
自抗扰控制器七-二阶 LADRC-PLL 结构设计
Dojo tutorials:getting started with deferrals source code and example execution summary
Two solutions of leetcode101 symmetric binary tree (recursion and iteration)
Huffman coding experiment report
Node.js: express + MySQL的使用
低代码平台国际化多语言(i18n)技术方案
Xctf mobile--app1 problem solving
Leetcode234 palindrome linked list
The latest version of lottery blind box operation version
强大的头像制作神器微信小程序
随机推荐
Two solutions of leetcode101 symmetric binary tree (recursion and iteration)
Integer case study of packaging
Analysis of the influence of voltage loop on PFC system performance
2022-01-27 research on the minimum number of redis partitions
Swift bit operation exercise
How to get user location in wechat applet?
Ten workplace rules
It feels great to know you learned something, isn‘t it?
2022-02-09 survey of incluxdb cluster
Analysis of a music player Login Protocol
Node.js: express + MySQL的使用
2022-01-27 redis cluster brain crack problem analysis
Quick learning 1.8 front and rear interfaces
Keep learning swift
【习题六】【数据库原理】
如何在微信小程序中获取用户位置?
【判断题】【简答题】【数据库原理】
【计网】第三章 数据链路层(2)流量控制与可靠传输、停止等待协议、后退N帧协议(GBN)、选择重传协议(SR)
Xctf mobile--app1 problem solving
阿里 & 蚂蚁自研 IDE