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.

分析:题意为计算32位整型数2进制表示中1的个数。

方法还是非常多的,也很好理解:

1、利用2进制的数学特点来

class Solution {
public:
int hammingWeight(uint32_t n) {
int count=0;
while(n){
if(n%2==1) count++;
n=n/2;
}
return count;
}
};

2、将每一位分别和1做与运算,计算不为0的个数即可

class Solution {
public:
int hammingWeight(uint32_t n) {
int count=0;
while(n){
count+=n&1;
n>>=1;
}
return count;
}
};

3、每次n&(n-1)可以将n里面的值为1的位数减少一位

class Solution {
public:
int hammingWeight(uint32_t n) {
int sum = 0;
while (n)
{
n &= (n-1);
++sum;
} return sum;
}
};

  

  

leetcode:Number of 1 Bits的更多相关文章

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

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

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

  3. LeetCode OJ:Number of 1 Bits(比特1的位数)

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

  4. LeetCode 191:number of one bits

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

  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 二进制数1的个数

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

  7. 【leetcode】Number of 1 Bits

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

  8. LeetCode 191 Number of 1 Bits

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

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

随机推荐

  1. idea maven添加jar包

    在“项目结构“里设置 选择libaray,添加jar包

  2. CoreText 使用教程

    [iOS开发] CoreText 使用教程:以创建一个简单的杂志应用为例抢沙发 分类:iPhone开发 标签:CoreText.iOS.iOS开发.iOS开发教程.杂志应用 BBS.CHINAAPP. ...

  3. 客户端发包 GS端接收

    客户端发包,GS接收 bool GameServer::ProcessLoop(packet& rPkt)//GS线程做的 { if(false == m_spDataLayer->Re ...

  4. [工作积累] error: bad class file magic (cafebabe) or version (0033.0000)

    Update Android SDK build tool to latest can solve my problem.

  5. ie 与 Chrome 时间格式化问题.

    ie 与 Chrome 时间格式化通用: new Date(res[i].Time.replaceAll("-", "/")).format("yyy ...

  6. 【ASP.Net MVC】AspNet Mvc一些总结

    AspNet Mvc一些总结 RestaurantReview.cs using System; using System.Collections.Generic; using System.Comp ...

  7. 关于JS中的constructor与prototype

    ======================================================================== 在学习JS的面向对象过程中,一直对constructo ...

  8. Android 添加Button事件

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentV ...

  9. Sqli-labs less 40

    Less-40 本关的sql语句为SELECT * FROM users WHERE id=('$id') LIMIT 0,1 我们根据sql语句构造以下的payload: http://127.0. ...

  10. zoj Fibonacci Numbers ( java , 简单 ,大数)

    题目 //f(1) = 1, f(2) = 1, f(n > 2) = f(n - 1) + f(n - 2) import java.io.*; import java.util.*; imp ...