原题链接在这里:https://leetcode.com/problems/number-of-1-bits/

题目:

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?

题解:

Bit manipulation每次右移一位 & 1.

unsigned number 右移 >>>. signed number 右移>>.

也可以使用Integer.bitCount() function.

Time Complexity: O(1). Space: O(1).

AC Java:

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

Method 3 是通过 n & (n-1)消掉最右侧的一个1.

消掉一个1, 对应的res就加一。

AC Java:

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

跟上Hamming DistanceCounting Bits.

LeetCode Number of 1 Bits的更多相关文章

  1. 2016.5.15——leetcode:Number of 1 Bits ,

    leetcode:Number of 1 Bits 代码均测试通过! 1.Number of 1 Bits 本题收获: 1.Hamming weight:即二进制中1的个数 2.n &= (n ...

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

  3. [LeetCode] Number of 1 Bits 位操作

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

  4. LeetCode——Number of 1 Bits

    //求一个整数的二进制串中1的个数 public int hammingWeight(int n) { String b_str = Integer.toBinaryString(n); int b_ ...

  5. LeetCode Number of 1 Bits 计算1的个数

    题意: 提供一个无符号32位整型uint32_t变量n,返回其二进制形式的1的个数. 思路: 考察二进制的特性,设有k个1,则复杂度为O(k).考虑将当前的数n和n-1做按位与,就会将n的最后一个1去 ...

  6. lc面试准备:Number of 1 Bits

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

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

  8. [LeetCode] Prime Number of Set Bits in Binary Representation 二进制表示中的非零位个数为质数

    Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...

  9. [LeetCode] Binary Number with Alternating Bits 有交替位的二进制数

    Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...

随机推荐

  1. BZOJ4383 : [POI2015]Pustynia

    设$a$到$b$的边权为$c$的有向边的含义为$b\geq a+c$,则可以根据题意构造出一张有向图. 设$f[x]$为$x$点可行的最小值,$a[x]$为$x$位置已知的值,则$f[x]=\max( ...

  2. ajax与HTML5 history pushState/replaceState实例

    一.本文就是个实例展示 三点: 我就TM想找个例子,知道如何个使用,使用语法什么的滚粗 跟搜索引擎搞基 自己备忘 精力总是有限的,昨天一冲动,在上海浦东外环之外订了个90米的房子,要借钱筹首付.贷款和 ...

  3. 栈式自动编码器(Stacked AutoEncoder)

    起源:自动编码器 单自动编码器,充其量也就是个强化补丁版PCA,只用一次好不过瘾. 于是Bengio等人在2007年的  Greedy Layer-Wise Training of Deep Netw ...

  4. Android GPS 取经纬度

    // 获取位置管理服务 private LocationManager locationManager;3 String mProviderName = ""; private v ...

  5. 【HDU】4405 Aeroplane chess

    http://acm.hdu.edu.cn/showproblem.php?pid=4405 题意:每次可以走1~6格,初始化在第0格,走到>=n的格子就结束.还有m个传送门,表示可以从X[i] ...

  6. 【noiOJ】P1996

    1996:登山 查看 提交 统计 提问 总时间限制:  5000ms 内存限制:  131072kB 描述 五一到了,PKU-ACM队组织大家去登山观光,队员们发现山上一个有N个景点,并且决定按照顺序 ...

  7. 【noiOJ】p1759

    1759:最长上升子序列 查看 提交 统计 提问 总时间限制:  2000ms 内存限制:  65536kB 描述 一个数的序列bi,当b1 < b2 < ... < bS的时候,我 ...

  8. Emoji表情符号录入MySQL数据库报错

    版本一: 1,查看tomcat后台日志,核心报错信息如下:   Caused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\ ...

  9. Flex Vector使用(转)

    从前(Flex SDK 4.0 以前版本)创建 Vector 实例一定要如下所示,这样的语法使我们在构造 Vector 对象时无法指定元素: var v:Vector.<T> = new ...

  10. ICE——1.Printer

    一:打印机 1.Slice定义: interface Printer { void printString(string s); }; 我们的Slice定义含有一个接口,叫作Printer.目前,我们 ...