当前位置:网站首页>剑指 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;
}
};
边栏推荐
- Method overloading and rewriting
- Alibaba is bigger than sending SMS (user microservice - message microservice)
- 最新版盲盒商城thinkphp+uniapp
- Xctf mobile--app3 problem solving
- ORM use of node -serialize
- Sqoop1.4.4原生增量导入特性探秘
- The foreground uses RSA asymmetric security to encrypt user information
- Project video based on Linu development
- Define a list, store n integers, and calculate the length, maximum value, minimum value and average value of the list
- Low code platform international multilingual (I18N) technical solution
猜你喜欢

【R】【密度聚类、层次聚类、期望最大化聚类】

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

Xctf mobile--app3 problem solving

Idea packages the web project into a war package and deploys it to the server to run

How to convert a decimal number to binary in swift

Togaf certification self-study classic v2.0

Detailed explanation of the most complete constraintlayout in history

【数据库原理复习题】

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

Public and private account sending prompt information (user microservice -- message microservice)
随机推荐
01 three solutions to knapsack problem (greedy dynamic programming branch gauge)
Seven second order ladrc-pll structure design of active disturbance rejection controller
Flinksql can directly create tables and read MySQL or Kafka data on the client side, but how can it automatically flow and calculate?
【数据库原理及应用教程(第4版|微课版)陈志泊】【第六章习题】
How to stand out quickly when you are new to the workplace?
【習題七】【數據庫原理】
[exercise 6] [Database Principle]
[combinatorics] permutation and combination (multiple set permutation | multiple set full permutation | multiple set incomplete permutation all elements have a repetition greater than the permutation
It feels great to know you learned something, isn‘t it?
ncnn神经网络计算框架在香橙派OrangePi 3 LTS开发板中的使用介绍
自抗扰控制器七-二阶 LADRC-PLL 结构设计
ImportError: No module named examples. tutorials. mnist
Kotlin notes - popular knowledge points asterisk (*)
Powerful avatar making artifact wechat applet
Application of ncnn Neural Network Computing Framework in Orange Pi 3 Lts Development Board
Dojo tutorials:getting started with deferrals source code and example execution summary
Tianyi ty1208-z brush machine detailed tutorial (free to remove)
【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素
Swift return type is a function of function
Glide 4.6.1 API initial