当前位置:网站首页>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;
}
};
边栏推荐
- Tensorflow binary installation & Failure
- C graphical tutorial (Fourth Edition)_ Chapter 18 enumerator and iterator: enumerator samplep340
- How to stand out quickly when you are new to the workplace?
- studio All flavors must now belong to a named flavor dimension. Learn more
- Gan totem column bridgeless boost PFC (single phase) seven PFC duty cycle feedforward
- 剑指 Offer 14- I. 剪绳子
- Ten workplace rules
- 并网-低电压穿越与孤岛并存分析
- 十條職場規則
- Huffman coding experiment report
猜你喜欢

Harmonic current detection based on synchronous coordinate transformation

Two solutions of leetcode101 symmetric binary tree (recursion and iteration)

Glide question you cannot start a load for a destroyed activity

有限状态机FSM

【Colab】【使用外部数据的7种方法】

Node.js: express + MySQL的使用

Leetcode234 palindrome linked list

Application of ncnn neural network computing framework in orange school orangepi 3 lts development board

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

Node. Js: use of express + MySQL
随机推荐
最新版盲盒商城thinkphp+uniapp
2022-01-27 redis cluster technology research
[combinatorics] permutation and combination (multiple set permutation | multiple set full permutation | multiple set incomplete permutation all elements have a repetition greater than the permutation
Define a list, store n integers, and calculate the length, maximum value, minimum value and average value of the list
Drop down refresh conflicts with recyclerview sliding (swiperefreshlayout conflicts with recyclerview sliding)
[exercise 5] [Database Principle]
Glide 4.6.1 API initial
SLF4J 日志门面
关于CPU缓冲行的理解
Ten workplace rules
The latest version of lottery blind box operation version
2022-01-27 research on the minimum number of redis partitions
Kotlin notes - popular knowledge points asterisk (*)
[exercise 6] [Database Principle]
2022-02-11 practice of using freetsdb to build an influxdb cluster
Nodejs+express+mysql realizes login function (including verification code)
Keep learning swift
剑指 Offer 12. 矩阵中的路径
解决 System has not been booted with systemd as init system (PID 1). Can‘t operate.
Application of ncnn neural network computing framework in orange school orangepi 3 lts development board