当前位置:网站首页>剑指 Offer 15. 二进制中1的个数
剑指 Offer 15. 二进制中1的个数
2022-07-03 12:10:00 【嗝~~~~】
剑指 Offer 15. 二进制中1的个数
难度简单251
编写一个函数,输入是一个无符号整数(以二进制串的形式),返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为 汉明重量).)。
提示:
- 请注意,在某些语言(如 Java)中,没有无符号整数类型。在这种情况下,输入和输出都将被指定为有符号整数类型,并且不应影响您的实现,因为无论整数是有符号的还是无符号的,其内部的二进制表示形式都是相同的。
- 在 Java 中,编译器使用 二进制补码 记法来表示有符号整数。因此,在上面的 示例 3 中,输入表示有符号整数 -3。
示例 1:
输入:n = 11 (控制台输入 00000000000000000000000000001011)输出:3解释:输入的二进制串
00000000000000000000000000001011 中,共有三位为 '1'。
示例 2:
输入:n = 128 (控制台输入 00000000000000000000000010000000)输出:1解释:输入的二进制串 00000000000000000000000010000000 中,共有一位为 '1'。
示例 3:
输入:n = 4294967293 (控制台输入 11111111111111111111111111111101,部分语言中 n = -3)输出:31解释:输入的二进制串 11111111111111111111111111111101 中,共有 31 位为 '1'。
提示:
- 输入必须是长度为 32 的 二进制串 。
代码
class Solution {
public:
int hammingWeight(uint32_t n) {
int flag=0;
while(n){
if(n&1==1)
flag++;
n=n>>1;
}
return flag;
}
};
边栏推荐
- 4. 无线体内纳米网:电磁传播模型和传感器部署要点
- initial、inherit、unset、revert和all的区别
- Ten workplace rules
- Keep learning swift
- The solution to change the USB flash disk into a space of only 2m
- 4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment
- ncnn神经网络计算框架在香橙派OrangePi 3 LTS开发板中的使用介绍
- [ArcGIS user defined script tool] vector file generates expanded rectangular face elements
- Quick learning 1.8 front and rear interfaces
- Attack and defense world mobile--ph0en1x-100
猜你喜欢

Cache penetration and bloom filter

【数据库原理及应用教程(第4版|微课版)陈志泊】【第四章习题】

Gan totem column bridgeless boost PFC (single phase) seven PFC duty cycle feedforward

(最新版) Wifi分销多开版+安装框架

【数据库原理及应用教程(第4版|微课版)陈志泊】【第三章习题】

Swift bit operation exercise

01 three solutions to knapsack problem (greedy dynamic programming branch gauge)

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

ncnn神经网络计算框架在香橙派OrangePi 3 LTS开发板中的使用介绍

Day 1 of kotlin learning: simple built-in types of kotlin
随机推荐
With pictures and texts, summarize the basic review of C language in detail, so that all kinds of knowledge points are clear at a glance?
【数据库原理及应用教程(第4版|微课版)陈志泊】【第五章习题】
Nodejs+Express+MySQL实现登陆功能(含验证码)
Differences and connections between final and static
Analysis of a music player Login Protocol
ImportError: No module named examples. tutorials. mnist
2021 autumn Information Security Experiment 1 (password and hiding technology)
Leetcode234 palindrome linked list
Swift5.7 扩展 some 到泛型参数
The upward and downward transformation of polymorphism
Airflow installation jump pit
社交社区论坛APP超高颜值UI界面
The latest version of blind box mall thinkphp+uniapp
Day 1 of kotlin learning: simple built-in types of kotlin
C graphical tutorial (Fourth Edition)_ Chapter 18 enumerator and iterator: enumerator samplep340
Node. Js: use of express + MySQL
Public and private account sending prompt information (user microservice -- message microservice)
Using swift language features, write a pseudo-random number generator casually
[ArcGIS user defined script tool] vector file generates expanded rectangular face elements
Attack and defense world mobile--ph0en1x-100