当前位置:网站首页>LeetCode 191. Number of 1 Bits(位1的个数) 位运算/easy
LeetCode 191. Number of 1 Bits(位1的个数) 位运算/easy
2022-07-27 14:05:00 【押切徹】
1.Description
编写一个函数,输入是一个无符号整数(以二进制串的形式),返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量)。
提示:
请注意,在某些语言(如 Java)中,没有无符号整数类型。在这种情况下,输入和输出都将被指定为有符号整数类型,并且不应影响您的实现,因为无论整数是有符号的还是无符号的,其内部的二进制表示形式都是相同的。
在 Java 中,编译器使用二进制补码记法来表示有符号整数。因此,在上面的 示例 3 中,输入表示有符号整数 -3。
2.Example
输入:00000000000000000000000000001011
输出:3
解释:输入的二进制串 00000000000000000000000000001011 中,共有三位为 '1'。
3.Solution
1.按位右移
可以右移32次或者右移到n等于0为止。
注意:Java中的“>>”是算数右移,舍弃最低位,高位用符号位填补;
“>>>”是逻辑右移:舍弃最低位,高位用 0 填补。
因此使用第二种方法时需要使用“>>>”。
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int count = 0;
while(n!=0) {
if((n&1)==1) {
count++;
}
n=n>>>1;
}
return count;
}
}
2.位运算优化
使用n = n&(n-1)可以得到将n的最低位的1变成0之后的数,因此只要循环进行操作统计一共进行了多少次操作就行了。
public class Solution {
public int hammingWeight(int n) {
int ret = 0;
while (n != 0) {
n &= n - 1;
ret++;
}
return ret;
}
}
边栏推荐
- Hdu3117 Fibonacci numbers [mathematics]
- 同花顺开户在手机开户安全吗?
- Stm32f103c8t6 drives sh1106 1.3 "IIC OLED display under Arduino frame
- Nefu117 number of prime numbers [prime number theorem]
- @What happens when bean and @component are used on the same class?
- Database storage series (1) column storage
- User question understanding and answer content organization for epidemic disease Science Popularization
- Idea makes jar packages and introduces jar packages
- Visual system design example (Halcon WinForm) -9. text display
- telnet远程登录aaa模式详解【华为eNSP】
猜你喜欢

对话框管理器第三章:创建控件

The interviewer asked: how to judge whether an element is in the visible area?
How to solve cache avalanche, breakdown and penetration problems

STM32 - capacitive touch button experiment

STM32F103C8T6在Arduino框架下驱动SH1106 1.3“ IIC OLED显示

User question understanding and answer content organization for epidemic disease Science Popularization
![[popular science] the difference and connection between accuracy and resolution](/img/12/efcce1f6b8801d8d8b08b79818632c.png)
[popular science] the difference and connection between accuracy and resolution

telnet远程登录aaa模式详解【华为eNSP】

Who can't capture packets these days? Wireshark packet capture and common protocol analysis are for you!

Lecture 4: Longest ascending substring
随机推荐
lc marathon 7.26
Jmeter录制接口自动化
C语言基础练习题目
视觉系统设计实例(halcon-winform)-9.文字显示
internship:其他配置类的编写
[popular science] the difference and connection between accuracy and resolution
Regular expressions: mailbox matching
adb命令 (安装apk包格式:adb install 电脑上apk地址包名)
软件产品第三方测试费用为什么没有统一的报价?
Web页面table表格,实现快速筛选
Confirm the time accuracy of the power supply setting voltage through the i/o function of vn1630/vn7640
Nefu119 combinatorial prime [basic theorem of arithmetic]
如何做好企业系统漏洞评估
网络设备硬核技术内幕 路由器篇 11 CISCO ASR9900拆解 (五)
网络设备硬核技术内幕 路由器篇 16 DPDK及其前传(一)
【云享读书会第13期】视频文件的封装格式
Graphical SQL is too vivid
Finally, someone finished all the dynamic planning, linked list, binary tree and string required for the interview
Forward proxy and reverse proxy
Research on Chinese idiom metaphorical knowledge recognition and relevance based on transfer learning and text enhancement