(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 ...
随机推荐
- getDimension,getDimensionPixelOffset和getDimensionPixelSize的一点说明
getDimension和getDimensionPixelOffset的功能类似, 都是获取某个dimen的值,但是如果单位是dp或sp,则需要将其乘以density 如果是px,则不乘.并且get ...
- java提供的默认list排序方法-转
1.java提供的默认list排序方法 主要代码: List<String> list = new ArrayList();list.add("刘媛媛"); list. ...
- 对 COM+ 组件进行了方法调用,但该组件有一个已被中止的或正在被中止的事务。 (异常来自 HRESULT:0x8004E003)
错误: 对 COM+ 组件进行了方法调用,但该组件有一个已被中止的或正在被中止的事务. (异常来自 HRESULT:0x8004E003) 解决办法: 程序连接的数据库换到本地. 具体原因没搞 ...
- C#本地时间和GMT(UTC)时间的转换
/// <summary> /// 本地时间转成GMT时间 /// </summary> 4 public static string ToGMTString(DateTime ...
- 【JdbcTemplete】JdbcTemplete代码详解--模板方法详解
JdbcTemplete类层次结构 JdbcAccessor:对DataSource数据源进行管理和配置: JdbcOperations:定义了通过JDBC操作数据库的基本操作方法: JdbcTemp ...
- linux脚本后台运行
一般情况下,linux运行脚本是随着终端的关闭而关闭的,那么怎么让脚本能够在后台运行并且不随终端关闭而关闭呢? 这时用到的是nohup命令 格式: nohup 脚本路径 & 例: nohup ...
- Install and configure Intel NIC teaming on R420
OS env: windows2008 R2 std 1. Download NIC driver from Dell Website http://www.dell.com/support/home ...
- Page.User.Identity.Name获取不到结果
如果在IIS部署后Page.User.Identity.Name获取不到值,需要检查以下设置: 1.web.config设置<authentication mode="Windows& ...
- ultraedit15.00.0.1046注册码
ultraedit注册码,版本:15.00.0.1043·········· 用户名 MAYBELOVE 注册码 LFKKM-KIMMX-OSFEB-PMISO-ELILS-IIIHO-KKHLR ...
- 尽量使用条件属性(Conditional Attribute)而不是#if/#endif预处理
http://www.cnblogs.com/JiangSoney/archive/2009/08/10/1543197.html .net框架提供了一个特性:属性(Attribute),注意:此属性 ...