(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 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.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
参考编程之美120页
方法1:使用位操作
代码如下:
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int num=0;
while(n!=0){
num+=(n&1);
n=n>>>1;
}
return num;
}
}
运行结果:时间复杂度为O(logV),即二进制位数。

方法二:n&=(n-1)
代码如下:
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int num=0;
while(n!=0){
n&=(n-1);
num++;
}
return num;
}
}
运行结果:

(easy)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 ...
- LeetCode 191 Number of 1 Bits
Problem: Write a function that takes an unsigned integer and returns the number of '1' bits it has ( ...
- 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 ...
- 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 ...
随机推荐
- UML学习(类图和序列图等)
visio绘制UML图使用visio 提示此UML形状所在的绘图页不是UML模型图的一部分 请问这个问题怎么解决?新建->选择绘图类型->选择软件与数据库模板->选择UML模型图-& ...
- WAS8.5安装
由于公司网络禁止上传图片,在网上找到相近的,安装过程图,可参照:http://www.open-open.com/doc/view/488fe6eaa2084da6b87cc18f1c00d2a8 1 ...
- Python基础教程【读书笔记】 - 2016/7/31
希望通过博客园持续的更新,分享和记录Python基础知识到高级应用的点点滴滴! 第十波:第10章 充电时刻 Python语言的核心非常强大,同时还提供了更多值得一试的工具.Python的标准安装包括 ...
- (转)textarea去掉右侧滚动条,去掉右下角拖拽
本文转载自:http://blog.csdn.net/cctv_end/article/details/7946188 代码: <TEXTAREA style= "overflo ...
- (转)mongodb分片
本文转载自:http://www.cnblogs.com/huangxincheng/archive/2012/03/07/2383284.html 在mongodb里面存在另一种集群,就是分片技术, ...
- 51nod1039 x^3 mod p
X*X*X mod P = A,其中P为质数.给出P和A,求<=P的所有X. Input 第1行:一个数T,表示后面用作输入测试的数的数量.(1 <= T <= 1000) 第2 ...
- Linux系统默认服务建议开启关闭说明列表
服务名称 功能简介 建议 acpid 电源管理接口.如果是笔记本用户建议开启,可以监听内核层的相关电源事件. 开启 anacron 系统的定时任务程序.cron的一个子系统,如果定时任务错过了执行时间 ...
- 黄聪:阿里云Windows2012服务器IIS8实现wordpress完美伪静态(ISAPIRewritev)
1.下载64位URL重写组件:http://www.iis.net/downloads/microsoft/url-rewrite (可以直接下载:urlrewrite2.rar) 2.暂停IIS ...
- 黄聪:PHP json_encode中文乱码解决方法
相信很多人在使用Ajax与后台php页面进行交互的时候都碰到过中文乱码的问题.JSON作为一种轻量级的数据交换格式,备受亲睐,但是用PHP作为后台交互,容易出现中文乱码的问题.JSON和js一样,对于 ...
- (WPF) MVVM: ComboBox Binding, XML 序列化
基本思路还是在View的Xmal里面绑定ViewModel的属性,虽然在View的后台代码中也可以实现binding,但是还是在Xmal里面相对的代码量要少一些. 此例子要实现的效果就是将一个List ...