当前位置:网站首页>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;
}
};
边栏推荐
- Create a dojo progress bar programmatically: Dojo ProgressBar
- [network counting] Chapter 3 data link layer (2) flow control and reliable transmission, stop waiting protocol, backward n frame protocol (GBN), selective retransmission protocol (SR)
- 【习题五】【数据库原理】
- Glide 4.6.1 API initial
- 基于Linu开发的项目视频
- initial、inherit、unset、revert和all的区别
- How to convert a decimal number to binary in swift
- studio All flavors must now belong to a named flavor dimension. Learn more
- 自抗扰控制器七-二阶 LADRC-PLL 结构设计
- Approve iPad, which wants to use your icloud account
猜你喜欢

最新版抽奖盲盒运营版

Method overloading and rewriting

阿里 & 蚂蚁自研 IDE

OpenHarmony应用开发之ETS开发方式中的Image组件
![[network counting] Chapter 3 data link layer (2) flow control and reliable transmission, stop waiting protocol, backward n frame protocol (GBN), selective retransmission protocol (SR)](/img/45/c2d7934b886d8090373ca9e6e23c97.gif)
[network counting] Chapter 3 data link layer (2) flow control and reliable transmission, stop waiting protocol, backward n frame protocol (GBN), selective retransmission protocol (SR)

Idea full text search shortcut ctr+shift+f failure problem

Application of ncnn Neural Network Computing Framework in Orange Pi 3 Lts Development Board

initial、inherit、unset、revert和all的区别
![[review questions of database principles]](/img/c3/81d192a40bcc4f5d72fcbe76c708bb.png)
[review questions of database principles]

(最新版) Wifi分销多开版+安装框架
随机推荐
Ali & ant self developed IDE
Differences between initial, inherit, unset, revert and all
剑指 Offer 15. 二进制中1的个数
context. Getexternalfilesdir() is compared with the returned path
Low code platform international multilingual (I18N) technical solution
Glide question you cannot start a load for a destroyed activity
[Exercice 5] [principe de la base de données]
Swift Error Handling
[combinatorics] permutation and combination (multiple set permutation | multiple set full permutation | multiple set incomplete permutation all elements have a repetition greater than the permutation
[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
Export the entire Oracle Database
【R】【密度聚类、层次聚类、期望最大化聚类】
基于Linu开发的项目视频
Two solutions of leetcode101 symmetric binary tree (recursion and iteration)
studio All flavors must now belong to a named flavor dimension. Learn more
2022-01-27 use liquibase to manage MySQL execution version
剑指 Offer 11. 旋转数组的最小数字
Four problems and isolation level of MySQL concurrency
Cache penetration and bloom filter
【数据库原理及应用教程(第4版|微课版)陈志泊】【第六章习题】