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. Leetcode#108 Convert Sorted Array to Binary Search Tree

    原题地址 对于已排序数组,二分法递归构造BST 代码: TreeNode *buildBST(vector<int> &num, int i, int j) { if (i > ...

  2. html5.js

    可以让IE8等不支持Html5的浏览器,支持Html5元素,比如<header> <footer> <section>等标签 /* HTML5 Shiv v3.7. ...

  3. 功率单位mW 和 dBm 的换算

    无线电发射机输出的射频信号,通过馈线(电缆)输送到天线,由天线以电磁波形式辐射出去.电磁波到达接收地点后,由天线接收下来(仅仅接收很小很小一部分功率),并通过馈线送到无线电接收机.因此在无线网络的工程 ...

  4. Sqli-labs less 38

    Less-38 学习了关于stacked injection的相关知识,我们在本关可以得到直接的运用. 在执行select时的sql语句为:SELECT * FROM users WHERE id=' ...

  5. linux权威指南 简记

    /proc 目录,linxu系统以文件形式存放进程信息,这是一个虚拟的文件系统,不占有任何磁盘空间,当读取该文件系统时,系统内核会拦截动作,并动态产生文件与目录的内容 查看该文件夹,会发现很多已数字命 ...

  6. 提权后获取linux root密码

    提权后获取linux root密码 2011-09-09 10:45:25     我来说两句      收藏    我要投稿 在webbackdoor本身是root(可能性小的可怜)或通过某漏洞溢出 ...

  7. Full GC有关问题学习分析(转载)

    网站持久代引发Full GC问题分析 现状: Dragoon(监控系统)的日报显示trade_us_wholelsale(美国wholesale集群),日均Young GC次数25w次左右,应用暂停2 ...

  8. Tomcat内存溢出(java.lang.OutOfMemoryError: PermGen space)的解决办法

    Tomcat启动时报如下错误: java.lang.OutOfMemoryError: PermGen space 解决办法: 配置相关内存大小.其中按照启动tomcat的不同方式,分如下三种情况 a ...

  9. JMeter使用指南--转

    JMeter使用指南 本文重点介绍JMeter工具在测试中地位以及其中一些难以理解或者手册中含糊不清的感念,读者可以通过本文了解这些概念,然后再根据自己的需要查阅JMeter中各个组件的具体用法来完成 ...

  10. Lua的require和module小结

    Lua的require和module小结  module特性是lua5.1中新增的,用于设置Lua文件自己的模块,最常用的方式是module(name,package.seeall),有时候lua文件 ...