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. javascript版Ajax请求

    什么是Ajax请求,Ajax也就是“Asynchronous JavaScript and XML”(异步JavaScript和XML),无刷新数据读取.能减少流量的消耗,也提高了浏览的流畅性,给用户 ...

  2. wifidog源码分析 - 初始化阶段

    Wifidog是一个linux下开源的认证网关软件,它主要用于配合认证服务器实现无线路由器的认证放行功能. wifidog是一个后台的服务程序,可以通过wdctrl命令对wifidog主程序进行控制. ...

  3. httpClient多线程请求

    使用httpClient可模拟请求Url获取资源,使用单线程的请求速度上会有一定的限制,参考了Apache给出的例子,自己做了测试实现多线程并发请求,以下代码需要HttpClient 4.2的包,可以 ...

  4. linux 上传/下载文件到windows工具

    一般来说,linux服务器大多是通过ssh客户端来进行远程的登陆和管理的,使用ssh登陆linux主机以后,如何能够快速的和本地机器进行文件的交互呢,也就是上传和下载文件到服务器和本地:   与ssh ...

  5. Java日志记录的事儿

    一.java日志组件 1.common-logging common-logging是apache提供的一个通用的日志接口.用户可以自由选择第三方的日志组件作为具体实现,像log4j,或者jdk自带的 ...

  6. NODEjs常见错误检查

    一.没有添加对uncaughtException异常的捕捉处理,最起码也要在其中写个日志记录错误,然后可以调用 process.exit(1); 退出进程. 二.处理函数的回调函数检查,经常忘记在回调 ...

  7. Static vs Dynamic Scope

    转自:http://hoolihan.net/blog-tim/2009/02/17/static-vs-dynamic-scope/ // start pseudo-code var y = &qu ...

  8. HTML5 webSQL

    https://www.ibm.com/developerworks/cn/web/1108_zhaifeng_websqldb/   <!DOCTYPE HTML> <html&g ...

  9. iOS视频压缩

    // // ViewController.m // iOS视频测试 // // Created by apple on 15/8/19. // Copyright (c) 2015年 tqh. All ...

  10. Spring mvc json null

    http://blog.csdn.net/zdsdiablo/article/details/9429263