LeetCode 191 Number of 1 Bits
Problem:
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).
For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011
, so the function should return 3.
Summary:
求三十二位无符号数含1的位数。
Analysis:
1. 最简单的思路为将n移位直至n为0,每移位一次判断最低位是否为1。但若有1在最高位则需移位32次,效率太低。
class Solution {
public:
int hammingWeight(uint32_t n) {
int cnt = ;
while (n) {
cnt += n & ;
n = n >> ;
} return cnt;
}
};
2. 下面这种思路能够把移位的次数降低至与含1的位数相等,大大提高了效率。
举个栗子:若n = 1010010,则:
- n = 1010010 n - 1 = 1010001 n & (n - 1) = 1010000
- n = 1010000 n - 1 = 1001111 n & (n - 1) = 1000000
- n = 1000000 n - 1 = 0111111 n & (n - 1) = 0000000
可以看出,每进行一次n & (n - 1)操作,最低位上的1就会被消去。
class Solution {
public:
int hammingWeight(uint32_t n) {
int cnt = ;
while (n) {
n &= (n - );
cnt++;
} return cnt;
}
};
LeetCode 191 Number of 1 Bits的更多相关文章
- Leetcode#191. Number of 1 Bits(位1的个数)
题目描述 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量). 示例 : 输入: 11 输出: 3 解释: 整数 11 的二进制表示为 000000 ...
- LN : leetcode 191 Number of 1 Bits
lc 191 Number of 1 Bits 191 Number of 1 Bits Write a function that takes an unsigned integer and ret ...
- LeetCode 191. Number of 1 bits (位1的数量)
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- [LeetCode] 191. Number of 1 Bits 二进制数1的个数
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- Java for LeetCode 191 Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- (easy)LeetCode 191.Number of 1 Bits
Number of 1 Bits Write a function that takes an unsigned integer and returns the number of ’1' bits ...
- Java [Leetcode 191]Number of 1 Bits
题目描述: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (als ...
- [LeetCode] 191. Number of 1 Bits ☆(位 1 的个数)
描述 Write a function that takes an unsigned integer and return the number of '1' bits it has (also kn ...
- leetcode 191 Number of 1 Bits(位运算)
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
随机推荐
- superF12
superF12是开发内嵌ie内核的桌面客户端时的一个调试工具
- PHP如何释放内存之unset销毁变量并释放内存详解
PHP的unset()函数用来清除.销毁变量,不用的变量,我们可以用unset()将它销毁.但是某些时候,用unset()却无法达到销毁变量占用的内存!我们先看一个例子: <?php $s = ...
- 第31天 mvp
interactor调用接口 Activity包含Presenter,这样界面上的操作就会通知到Presenter. Presenter调用view接口, Activity实现view接口,这样Pre ...
- Windows下MySQL 5.6安装及配置详细图解
一.安装前的准备 1.下载安装程序包,可到MySQL官方网站http://www.mysql.com/下载,如图1-1: 图1-1 下载后的安装文件如图1-2所示: 图1-2 二.安装 1.双击下载的 ...
- sublime linux下无法输入中文
cd ~ vim sublime_imfix.c 输入 #include <gtk/gtkimcontext.h> void gtk_im_context_set_client_windo ...
- android GestureDetector 手势基础
1. 当用户触摸屏幕的时候,会产生许多手势,例如down,up,scroll,filing等等,我们知道View类有个View.OnTouchListener内部接口,通过重写他的onTouch(Vi ...
- word文档的生成、修改、渲染、打印,使用Aspose.Words
无需MS Word也可执行各种文档处理任务,包括文档的生成.修改.渲染.打印,文档格式转换和邮件合并等文档处理.
- HDU 1174 爆头(计算几何)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1174 解题报告:就是用到了三维向量的点积来求点到直线的距离,向量(x1,y1,z1)与(x2,y2,z ...
- Intent flag 与启动模式的对应关系
Activity有四种启动模式: 1.standard(标准) 2.singleTop 3.singleTask 4.singleInstance 标识某个Activity的启动模式,有 ...
- Mysql报错Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist
安装mysql后,启动时候没有启动成功,查看了下日志报错如下:---------------------------------------------1 可以:初始化mysql:mysql_in ...