191. Number of 1 Bits

Easy

Write a function that takes an unsigned integer and return the number of '1' bits it has (also known as the Hamming weight).

Example 1:

Input: 00000000000000000000000000001011
Output: 3
Explanation: The input binary string 00000000000000000000000000001011 has a total of three '1' bits.

Example 2:

Input: 00000000000000000000000010000000
Output: 1
Explanation: The input binary string 00000000000000000000000010000000 has a total of one '1' bit.

Example 3:

Input: 11111111111111111111111111111101
Output: 31
Explanation: The input binary string 11111111111111111111111111111101 has a total of thirty one '1' bits.

Note:

  • Note that in some languages such as Java, there is no unsigned integer type. In this case, the input will be given as signed integer type and should not affect your implementation, as the internal binary representation of the integer is the same whether it is signed or unsigned.
  • In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 3 above the input represents the signed integer -3.

Follow up:

If this function is called many times, how would you optimize it?

package leetcode.easy;

public class NumberOf1Bits {
// you need to treat n as an unsigned value
public int hammingWeight1(int n) {
int bits = 0;
int mask = 1;
for (int i = 0; i < 32; i++) {
if ((n & mask) != 0) {
bits++;
}
mask <<= 1;
}
return bits;
} public int hammingWeight2(int n) {
int sum = 0;
while (n != 0) {
sum++;
n &= (n - 1);
}
return sum;
} @org.junit.Test
public void test() {
System.out.println(hammingWeight1(-3));
System.out.println(hammingWeight2(-3));
}
}

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

  1. [LeetCode] Number of 1 Bits 位1的个数

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

  2. 【leetcode】Number of 1 Bits

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

  3. Number of 1 Bits(Difficulty: Easy)

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

  4. LeetCode 191 Number of 1 Bits

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

  5. LeetCode Number of 1 Bits

    原题链接在这里:https://leetcode.com/problems/number-of-1-bits/ 题目: Write a function that takes an unsigned ...

  6. leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number

    一:Number of 1 Bits 题目: Write a function that takes an unsigned integer and returns the number of '1' ...

  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. leetcode:Number of 1 Bits

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

随机推荐

  1. Java中String、StringBuffer、StringBuilder、StringTokenizer的区别

    Java语言中,有4个类可以对字符或字符串进行操作,它们是Character.String.StringBuffer.StringTokenizer,其中Character用于单个字符操作,Strin ...

  2. 带有EXE参数的启动

    (一).先制作一个带启动参数的EXE文件. 步骤:            1.定义全局私有变量:private string[] s = new string[1];  //这里为了简单起见,只做一个 ...

  3. MySQL5.7版本安装(压缩包形式)

    1.去官网下载 MySQL 压缩包 2.配置环境变量 3.创建配置文件my.ini (放置 mysql-5.7.28-winx64 目录下) my.ini 配置文件 编写如下内容 [client] p ...

  4. [hdoj6483][莫队+线段树/ST]

    A Sequence Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  5. 17.组件页面应用和vue项目生成

    基本示例 这里有一个 Vue 组件的示例: // 定义一个名为 button-counter 的新组件 Vue.component('button-counter', { data: function ...

  6. WebService操作

    webservice是一种服务器通信技术,封装了socket.

  7. mysql查询重复数据

    SELECT * FROM oa_user ) ORDER BY UserName oa_user表名,UserName需要查重复的字段名

  8. Oracle 控制文件损坏解决方案

    Oracle 控制文件损坏解决方案 故障一:丢失(损坏)一个控制文件 前台报错:ORA-00205:error in identifying control file,check alert log ...

  9. filter方法求出列表所有奇数并构造新列

    a = [, , , , , , , , , ] b = filter(lambda x: x % != , a) for i in b: print(i)

  10. OpenFOAM 中边界条件的设定【转载】

    转载自:http://blog.sina.com.cn/s/blog_a0b4201d0102v7jt.html 用习惯了FLUENT的操作界面,再使用OpenFOAM就会觉得非常繁琐.遇到的第一个问 ...