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.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver

    SpringBoot运行报错——java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or rep ...

  2. 0021SpringMVC环境搭建及入门程序编写

    环境搭建: 1.创建项目 创建maven项目,勾选上Create from archetype,然后选中webapp再点击下一步,如下图: 解决项目创建过慢问题: 在创建maven项目过程中加入一组键 ...

  3. archlinux 使用 postgresql

    一.安装与初始化 1.初始化数据目录 默认安装后已创建 postgres 系统用户 切换到 postgres 用户 $ sudo -iu postgres # Or su - postgres for ...

  4. 文件读写(三)利用FileStream类操作字节数组byte[]、BinaryFormatter、内存流MemoryStream

    一.Stream类概述 在.NET Framework中,文件和流是有区别的.文件是存储在磁盘上的数据集,它具有名称和相应的路径.当打开一个文件并对其进行读/写时,该文件就称为流(stream).但是 ...

  5. 【xsy2978】Product of Roots 生成函数+多项式ln+多项式exp

    题目大意:给你两个多项式$f(x)$和$g(x)$,满足$f(x)=\prod\limits_{i=1}^{n}(a_i+1)$,$g(x)=\prod\limits_{i=1}^{m}(b_i+1) ...

  6. 使用Costura.Fody插件将自己写的程序打包成一个可以独立运行的EXE文件

    我们在开发程序的时候会引用很多DLL文件,在程序完成编写后,如果不把这些引用的DLL打包,不能在其他电脑运行,那么很多同学可能在想了,能不能把我们编写好的程序打包成一个EXE文件,最好双击就能运行,当 ...

  7. 在mysql中用int类型存储IP

    SELECT INET_ATON( '127.0.0.1' ); SELECT INET_NTOA();

  8. P5431 【模板】乘法逆元2

    洛谷题目链接 刚开始做乘法逆元还是有点懵逼的~ 以下式子都在模\(p\)意义下进行 我们把式子改一下,变成:\[\sum\limits_{i=1}^nk^i\times a_i^{-1}\] 我们先算 ...

  9. luogu 4211

    题意 存在一棵树,每次询问 \(l, r, z\) 求 \[\sum_{i = l} ^ {r} deep(lca(i, z))\] 考虑 lca 的实质:两点到根的路径的交集中深度最大的点 其中一点 ...

  10. [Bob]Collectors Problem

    https://vjudge.net/problem/UVA-10779#author=0 网络流 1.Bob向他有的贴纸连边,流量为他有的贴纸数量 2.每一种贴纸向汇点连流量为1的边 3.其余人,如 ...