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.

计算一个数字的二进制表示中含有的bit1的个数,简单的右移而已,代码如下所示:

 class Solution {
public:
int hammingWeight(uint32_t n) {
if(n == ) return ;
int ret = ;
while(n != ){
if(n & == ){
ret++;
}
n >>= ;
}
return ret;
}
};

LeetCode OJ:Number of 1 Bits(比特1的位数)的更多相关文章

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

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

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

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

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

  5. 【leetcode】Number of 1 Bits

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

  6. LeetCode 191 Number of 1 Bits

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

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

  8. (easy)LeetCode 191.Number of 1 Bits

    Number of 1 Bits Write a function that takes an unsigned integer and returns the number of ’1' bits ...

  9. leetcode:Number of 1 Bits

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

  10. Java [Leetcode 191]Number of 1 Bits

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

随机推荐

  1. idea中 在接口中如何直接跳转到该接口的是实现类中?

    例如,我想跳转到UserInfoDao 这个接口的实现类中,操作如下: 把鼠标放到这个接口UserInfoDao 上面,右键,选择 GO To ,然后选择 Implementations,就可以直接跳 ...

  2. Method Swizzling 剖析

    一.背景介绍 关于Method Swizzling的文章一大堆,讲的非常好的也数不胜数.不过,很多人只是会用,知道一些注意点.深入一点问的话,估计就答得不好.归其原因就是对Method Swizzli ...

  3. ubuntu下通过mono+jexus布署mvc5网站

    本文使用的ubuntu为14.04 LTS 一.安装mono,本文使用源码安装的方式 1.搭架mono编译环境 sudo apt-get update sudo apt-get install bui ...

  4. adb常用操作

    1.安装程序 adb -s serialno install -r path 2.切换电源 adb -s serialno shell input keyevent 26 3.主页键 adb -s s ...

  5. hadoop HA + kerberos HA集群搭建问题和测试总结

    1.  常见问题 (1)hostname设置问题.vi /etc/sysconfig/network (2)集群/etc/hosts没有统一. (3)yarn slave需要单独启动../sbin/y ...

  6. saltstack1

    saltstack三种运行模式: local本地.master/minion(类似于agent).salt ssh saltstack三大功能: 远程执行.配置管理.云管理 saltstack安装:1 ...

  7. Linux简单编程学习心得

    在Linux环境下简单编程学习心得 linux编程过程 在上周的<信息安全设计基础>的课程学习中学习到了在虚拟的linux环境下简单的编程.学习过程中接触到了vim.gcc和gcd在实验楼 ...

  8. Java使用BigDecimal解决精确计算的问题

    最近有人在微信上给我发了一个数学题目,如下图: 我看了之后感觉很是简单,但是却想了半天才解出来.解出来后我想到了用程序再解一遍,然而精确计算的问题却让人头疼不已. 解题思路: 思路其实很简单,暴力求解 ...

  9. 爬虫之xpath

    什么是XML XML 指可扩展标记语言(EXtensible Markup Language) XML 是一种标记语言,很类似 HTML XML 的设计宗旨是传输数据,而非显示数据 XML 的标签需要 ...

  10. android的wifi程序随笔作业

    不用说,做前最好新建一个wifiadmin类,用来装载你所有的wifi打开关闭,wifi配置,连接情况等等wifi操作,然后main类里做一些button连接listview显示wifi网络连接等东西 ...