当前位置:网站首页>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;
}
};
边栏推荐
- 高效能人士的七个习惯
- Brief introduction to mvcc
- Quickly learn member inner classes and local inner classes
- 【数据库原理及应用教程(第4版|微课版)陈志泊】【第四章习题】
- 低代码平台国际化多语言(i18n)技术方案
- 【数据库原理及应用教程(第4版|微课版)陈志泊】【第七章习题】
- Powerful avatar making artifact wechat applet
- 【Colab】【使用外部数据的7种方法】
- Glide 4.6.1 API initial
- C graphical tutorial (Fourth Edition)_ Chapter 18 enumerator and iterator: enumerator samplep340
猜你喜欢

The latest version of lottery blind box operation version

Swift bit operation exercise

Node. Js: use of express + MySQL

最新版抽奖盲盒运营版
![[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

低代码平台国际化多语言(i18n)技术方案

Social community forum app ultra-high appearance UI interface

Drop down refresh conflicts with recyclerview sliding (swiperefreshlayout conflicts with recyclerview sliding)

【综合题】【数据库原理】
![[review questions of database principles]](/img/c3/81d192a40bcc4f5d72fcbe76c708bb.png)
[review questions of database principles]
随机推荐
十條職場規則
Xctf mobile--app3 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
sitesCMS v3.1.0发布,上线微信小程序
2022-01-27 redis cluster brain crack problem analysis
Simple use and precautions of kotlin's array array and set list
阿里 & 蚂蚁自研 IDE
[data mining review questions]
Oh my Zsh + TMUX installation
initial、inherit、unset、revert和all的区别
[exercice 7] [principe de la base de données]
I'm too lazy to write more than one character
Swift return type is a function of function
Harmonic current detection based on synchronous coordinate transformation
【习题六】【数据库原理】
[ArcGIS user defined script tool] vector file generates expanded rectangular face elements
Approve iPad, which wants to use your icloud account
SLF4J 日志门面
Deeply understand the mvcc mechanism of MySQL
[exercise 7] [Database Principle]