LeetCode Number of 1 Bits

时间:2021-08-10 01:01:10

原题链接在这里: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&period;5&period;15——leetcode&colon;Number of 1 Bits ,

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

  2. &lbrack;LeetCode&rsqb; 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. &lbrack;LeetCode&rsqb; 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面试准备&colon;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&period; 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. &lbrack;LeetCode&rsqb; 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. &lbrack;LeetCode&rsqb; Binary Number with Alternating Bits 有交替位的二进制数

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

随机推荐

  1. C&num; 把背景为白色的图片变成透明图片

    Image Imageimage; Imageimage = System.Drawing.Image.FromFile(@"C:\A.JPG"); Bitmap bitmap = ...

  2. python装饰器示例

    https://wiki.python.org/moin/PythonDecoratorLibrary#Property_Definition

  3. add &period;json handler support in IIS 7

    Sometimes we need to create JSON in a text file with extension .json, however by default IIS 7 or an ...

  4. 【第一篇】说说MVC&plus;EF easyui dataGrid 动态加载分页表格

    首先上javascript的代码 <script type="text/javascript"> $(function () { LoadGrid(); }) //加载 ...

  5. JavaBean中DAO设计模式介绍&lpar;转&rpar;

    一.信息系统的开发架构 客户层-------显示层-------业务层---------数据层---------数据库 1.客户层:客户层就是客户端,简单的来说就是浏览器. 2.显示层:JSP/Ser ...

  6. Eclipse maven git

    http://www.blogjava.net/youxia/archive/2013/12/29/408182.html

  7. c&plus;&plus;基础 之 面向对象特征一 : 继承

    class Base { public: void f() { cout<<"void f()"<<endl<<endl; } void f(i ...

  8. RSA----实际函数库选择

    需求:对字符串加密 加密后不要超过这个字符串的长度,最好是1半的长度. 非对称算法. 重复度一定要低 1使用RSA加密 1   rsaeuro 2openssl    参考openssl编程 3 Cr ...

  9. 【Xilinx-Petalinux学习】-00-开始

    基于自己的ZYNQ板子,在上面运行petalinux,已经搞得稳定了,之后详细记录. 现在功能:QSPI启动u-boot和kernel,vdma.tpg.osd.vtc等IP模块在Linux下的驱动, ...

  10. 更换包管理工具npm为yarn

    官网:https://yarnpkg.com/zh-Hans/ 主要考虑: 1. npm管理安装模块依赖的版本不太方便,容易在删除node_modules重新install或在其他机器上新安装时, 安 ...