当前位置:网站首页>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;
}
};
边栏推荐
- 十條職場規則
- Social community forum app ultra-high appearance UI interface
- Nodejs+express+mysql realizes login function (including verification code)
- Using swift language features, write a pseudo-random number generator casually
- 剑指 Offer 16. 数值的整数次方
- The upward and downward transformation of polymorphism
- The latest version of blind box mall thinkphp+uniapp
- 【Colab】【使用外部数据的7种方法】
- ncnn神經網絡計算框架在香柳丁派OrangePi 3 LTS開發板中的使用介紹
- [combinatorics] permutation and combination (multiple set permutation | multiple set full permutation | multiple set incomplete permutation all elements have a repetition greater than the permutation
猜你喜欢

How to convert a decimal number to binary in swift

Xctf mobile--rememberother problem solving

IDEA 全文搜索快捷键Ctr+Shift+F失效问题

【计网】第三章 数据链路层(2)流量控制与可靠传输、停止等待协议、后退N帧协议(GBN)、选择重传协议(SR)

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

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

如何在微信小程序中获取用户位置?
![[ArcGIS user defined script tool] vector file generates expanded rectangular face elements](/img/39/0b31290798077cb8c355fbd058e4d3.png)
[ArcGIS user defined script tool] vector file generates expanded rectangular face elements

Social community forum app ultra-high appearance UI interface

【Colab】【使用外部数据的7种方法】
随机推荐
initial、inherit、unset、revert和all的区别
Swift5.7 extend some to generic parameters
[exercise 6] [Database Principle]
【数据库原理及应用教程(第4版|微课版)陈志泊】【第四章习题】
Brief introduction to mvcc
【数据库原理及应用教程(第4版|微课版)陈志泊】【第五章习题】
剑指 Offer 15. 二进制中1的个数
【数据库原理及应用教程(第4版|微课版)陈志泊】【第三章习题】
十條職場規則
Swift bit operation exercise
Leetcode234 palindrome linked list
(最新版) Wifi分销多开版+安装框架
Analysis of a music player Login Protocol
【数据挖掘复习题】
OpenHarmony应用开发之ETS开发方式中的Image组件
高效能人士的七个习惯
Grid connection - Analysis of low voltage ride through and island coexistence
2022-01-27 redis cluster brain crack problem analysis
studio All flavors must now belong to a named flavor dimension. Learn more
[comprehensive question] [Database Principle]