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.

Analysis:

This problem is like a magic, it could teach you a mgic skill in bit operation.

The instant idea:
Let us count the bit one by one, the easy way is to use a dividend (initial value is 2^31).
Theoretically, all integer could be represented in the form :
digit(31)*2^31 + digit(30)*2^30 + digit(29)*2^29 + ...
It could be solved by following pattern.
Assume: dividen = 2^i
digit = n / dividen (n is no larger than 2^(i+1))
The digit is the digit at 'i' index.
For next index, we need update dividen
dividen = dividen / 2; However that's just theoretical way!!!-.- Wrong solution: public int hammingWeight(int n) {
int count = 0;
long dividen = 1;
int digit = 0;
for (int i = 0; i < 31; i++)
dividen = dividen * 2;
while (n != 0) {
digit = n / dividen;
if (digit == 1)
count++;
n = n % dividen;
dividen = dividen / 2;
}
return count;
}
Wrong case:
Input:
1 (00000000000000000000000000000001)
Output:
0
Expected:
1 Reason:
For ordinary integer, it has reserved one bit for indicate 'negative' or 'positive' of a number. Only 31 bit could be used for representing digits.
Positive Range: [0, 2^31-1]
Negative Range: [-1, -(2^31)]
Why negtaive could reach 2^31, cause for "0000...000", it is no need for representing '-0', thus we use it for representing '-(2^31)'. Reference:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html Thus the following code could exceed range of positive number.
----------------------------------------------------------------------
for (int i = 0; i < 31; i++)
dividen = dividen * 2;
---------------------------------------------------------------------- A magic way to solve this problem:
Skill:
How to wipe out the last '1' of a integer?
n = n & (n-1)
Reason:
n-1 would turn the last '1' into '0', and all '0' after it into '1'.
Then we use '&', to keep recovering all bits except the last '1'. (has already been changed into '0')
Case:
n = 11110001000
&
n - 1 = 11110000000
ans = 11110000000 Great! Right! Don't mix this skill with
n = n ^ (n-1)
Which could keep the rightmost '1' bit only, all other bit were set into '0'.
Reference:
http://www.cnblogs.com/airwindow/p/4765145.html

Solution:

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

[LeetCode#191]Number of Bits的更多相关文章

  1. C#版 - Leetcode 191. Number of 1 Bits-题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

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

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

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

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

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

  6. LeetCode 191 Number of 1 Bits

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

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

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

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

随机推荐

  1. Oracle创建存储过程、执行存储过程基本语法

    >>>>>>>>>>>>>>>>>>>>>>>>> ...

  2. Bootstrap学习——起步

    一,前言 个人不是专业从事前端开发,但在一个小公司里工作,作为有过这样经历的程序员都知道,开发一个网站或者是一个管理系统,程序员基本所有的事都包了,真是什么都要懂一点啊.而我个人也不怎么喜欢写CSS和 ...

  3. U3D 摄像机镜头控制

    如果要实现,摄像机跟随着主角运动,还有运用滚轮实现镜头的方法和缩小的实现原理 方法1:把主摄像机放到主角的下面,作为一个子对象,调整好摄像机的视角,此时就会跟随了. 方法2:用代码让摄像机的相关的po ...

  4. 功能点分析法FPA笔记

    转载请注明出处:http://www.cnblogs.com/lidabnu/p/5700412.html 主要参考资料来自百度文库:http://wenku.baidu.com/link?url=y ...

  5. javascript-设置div隐藏

    html code: <div class="title"> <ul id="col02_left_title"> <li> ...

  6. iis最大连接数和队列长度

    最近公司项目的服务器出现了性能问题,关于iis负载过大,当客户问到最大连接数相关问题造成很萌的感觉,就查了相关资料: 首先让我们来看看IIS里面的这2个数字:最大并发连接数,队列长度.先说这2个数字在 ...

  7. 通过html5的range属性动态改变图片的大小

    range属性已经是很成熟的属性了,我们可以使用这个属性进行动态调整图片的宽度,其中原理在于通过不断获取range的值,并赋予给所需要的图片,进而达到动态改变图片的效果.下面贴出具体的代码,主要参照了 ...

  8. VS番茄助手安装(vs2015+vs2010):卸载之前的vs助手再安装新版本

    1 卸载之前的vs助手 vs2010: vs2015: 2 安装新版本

  9. Asp.net服务器控件在IE10下的不兼容问题

    Asp.net服务器控件在IE10下的不兼容问题 时间:2013-05-16 09:07点击: 89 次 [大 中 小] 相信很多使用IE10的童鞋们已经发现了这个问题,以下是本人在IE10标准模式下 ...

  10. css3多行省略号

    -webkit-line-clamp 概述: -webkit-line-clamp 是一个 不规范的属性(unsupported WebKit property),它没有出现在 CSS 规范草案中. ...