题目:

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.

链接:  http://leetcode.com/problems/number-of-1-bits/

题解:

跟上题一样,应该有些很厉害的解法。自己只做了最naive的一种。

Time Complexity - O(1), Space Complexity - O(1)

public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int count = 0; for(int i = 0; i < 32; i++)
if(((n >> i) & 1) == 1)
count++; return count;
}
}

Brian Kernighan的方法

public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int count = 0; for (count = 0; n != 0; count++) {
n &= n - 1; // clear the least significant bit set
} return count;
}
}

二刷:

Java:

public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int count = 0;
for (int i = 0; i < 32; i++) {
if (((n >> i) & 1) == 1) {
count++;
}
}
return count;
}
}
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int count = 0;
while (n != 0) {
n &= n - 1;
count++;
}
return count;
}
}

三刷:

使用一个while 循环,每次把n的最低的非0位清零。这样比计算全32位计算的位数少,但也多了每次按位与&操作的比较。

Java:

Time Complexity - O(1), Space Complexity - O(1)

public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int count = 0;
while (n != 0) {
n &= n - 1;
count++;
}
return count;
}
}

Reference:

http://www.hackersdelight.org/hdcodetxt/pop.c.txt

https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetNaive

191. Number of 1 Bits的更多相关文章

  1. Leetcode#191. Number of 1 Bits(位1的个数)

    题目描述 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量). 示例 : 输入: 11 输出: 3 解释: 整数 11 的二进制表示为 000000 ...

  2. 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 ...

  3. LeetCode 191 Number of 1 Bits

    Problem: Write a function that takes an unsigned integer and returns the number of '1' bits it has ( ...

  4. 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 ...

  5. (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 ...

  6. 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 ...

  7. 191. Number of 1 Bits Leetcode Python

    Write a function that takes an unsigned integer and returns the number of '1' bits it has (also know ...

  8. 【LeetCode】191. Number of 1 Bits

    题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...

  9. 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 ...

随机推荐

  1. windows下redis 开机自启动

    1,在redis的目录下执行(执行后就作为windows服务了) redis-server --service-install redis.windows.conf 2,安装好后需要手动启动redis ...

  2. dtcms,header显示头像和用户名,QQ互联老不通过的解决方法

    http://bbs.dtsoft.net/forum.php?mod=viewthread&tid=1742&extra=page%3D1

  3. spring-cloud-feign案例

    主要依赖 <dependencyManagement> <dependencies> <dependency> <groupId>org.springf ...

  4. CentOS系统中手动调整系统时间的方法

    我们一般使用“date -s”命令来修改系统时间.比如将系统时间设定成1996年6月10日的命令如下. #date -s 06/10/96 将系统时间设定成下午1点12分0秒的命令如下. #date ...

  5. window.print打印指定div实例代码

    window.print可以打印网页,但有时候我们只希望打印特定控件或内容,怎么办呢,请看下面的例子 首先我们可以把要打印的内容放在div中,然后用下面的代码进行打印. 复制代码代码如下: <h ...

  6. 通过 SuperObject 生成 json string

    (* { "name": "Henri Gourvest", /* this is a comment */ "vip": true, &q ...

  7. delphi 自带报告内存泄漏

    //报告内存泄漏 ReportMemoryLeaksOnShutdown := true;

  8. Spark Streaming揭秘 Day14 State状态管理

    Spark Streaming揭秘 Day14 State状态管理 今天让我们进入下SparkStreaming的一个非常好用的功能,也就State相关的操作.State是SparkStreaming ...

  9. wxPython 基本框架与运行原理 -- App 与 Frame

    <wxPython in Action> chapter 1.2 笔记 wxPython 是 wxWidgets 的 Python 实现,“w” for Microsoft Windows ...

  10. 通过telnet命令进行网络邮件发送

    1.建立smtp邮箱服务连接 open smtp.sina.com 2.连接上邮箱服务后进行握手操作 helo smtp.sina.com 3.输入帐号密码进行验证::此步后缓冲区会输出一些字符,你只 ...