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 ...
随机推荐
- [译]AngularJS中几种Providers(Factory, Service, Provider)的区别
原文: http://blog.xebia.com/2013/09/01/differences-between-providers-in-angularjs/ 什么是Provider? Angula ...
- 终端改变host的类型,还原
- 【开源一个小工具】一键将网页内容推送到Kindle
最近工作上稍微闲点,这一周利用下班时间写了一个小工具,其实功能挺简单但也小折腾了会. 工具名称:Simple Send to Kindle Github地址:https://github.com/zh ...
- isNaN() 确认是否是数字
isNaN(x): 当变量 x 不是数字,返回 true: 当变量 x 是其他值,(比如,1,2,3),返回false.
- 大数据之sqoop
一.安装: 1 解压然后把mysql的驱动放在$SQOOP_HOME/lib 目录中2. conf/sqoop-en.sh export HADOOP_COMMON_HOME=/home/hadoop ...
- setImageResource和setBackgroundResource的區別
它們的原型如下:void android.widget.ImageView.setImageResource(int resId)void android.view.View.setBackgroun ...
- Java Io(数据输入输出流)
Java Io 字节流中的DataInputStream 和 DataOutputStream,使用流更加方便,是流的一个扩展,更方便读取int, long,字符等类型数据. 事例代码如下: pack ...
- << CocoaPods安装和使用教程 >>github code4app以及cocoachina 苹果官方文档
developer.apple.com 英文搜索各个技术的官方介绍文档, 前提是英文过关 cocoachina ios最新新闻, 信息 code4app上有许多组件 http://www.code4a ...
- django的views里面的request对象详解大全
简介 HTTP 应用的信息是通过 请求报文 和 响应报文 传递的,关于更多的相关知识,可以阅读<HTTP权威指南>获得. 其中 请求报文 由客户端发送,其中包含和许多的信息,而 djang ...
- ext grid 使用combo,不显示display显示value问题
{ text: "附件类型", width: 150, dataIndex: 'uploadType', sortable: true, align: 'left', editor ...