当前位置:网站首页>剑指 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;
}
};
边栏推荐
- Alibaba is bigger than sending SMS (user microservice - message microservice)
- Xctf mobile--app3 problem solving
- 如何在微信小程序中获取用户位置?
- Glide question you cannot start a load for a destroyed activity
- Summary of error prone knowledge points: Calculation of define s (x) 3*x*x+1.
- ncnn神經網絡計算框架在香柳丁派OrangePi 3 LTS開發板中的使用介紹
- Analysis of a music player Login Protocol
- Create a dojo progress bar programmatically: Dojo ProgressBar
- A large select drop-down box, village in Chaoyang District
- Idea packages the web project into a war package and deploys it to the server to run
猜你喜欢

Analysis of a music player Login Protocol

最新版抽奖盲盒运营版

Integer case study of packaging

The latest version of lottery blind box operation version
![[comprehensive question] [Database Principle]](/img/d7/8c51306bb390e0383a017d9097e1e5.png)
[comprehensive question] [Database Principle]

Glide question you cannot start a load for a destroyed activity
![[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](/img/9d/6118b699c0d90810638f9b08d4f80a.jpg)
[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

Cache penetration and bloom filter

Xctf mobile--app3 problem solving

有限状态机FSM
随机推荐
【数据库原理及应用教程(第4版|微课版)陈志泊】【第三章习题】
Method overloading and rewriting
【习题五】【数据库原理】
C graphical tutorial (Fourth Edition)_ Chapter 17 generic: genericsamplep315
SSH登录服务器发送提醒
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?
idea将web项目打包成war包并部署到服务器上运行
OpenStack节点地址改变
Nodejs+express+mysql realizes login function (including verification code)
Record your vulnhub breakthrough record
如何在微信小程序中获取用户位置?
The latest version of blind box mall thinkphp+uniapp
[Exercice 5] [principe de la base de données]
【数据挖掘复习题】
高效能人士的七个习惯
Comprehensive evaluation of double chain notes · Siyuan notes: advantages, disadvantages and evaluation
(latest version) WiFi distribution multi format + installation framework
Swift bit operation exercise
【Colab】【使用外部数据的7种方法】
context. Getexternalfilesdir() is compared with the returned path