191. Number of 1 Bits

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位中有多少位值为1.

代码如下:

 class Solution {
public:
int hammingWeight(uint32_t n) {
unsigned pos = << ;
int count = ;
while(pos)
{
if(pos & n)
{
count++;
}
pos >>= ;
}
return count;
}
};

leetcode 191的更多相关文章

  1. C#版 - Leetcode 191. Number of 1 Bits-题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

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

  3. Leetcode#191. Number of 1 Bits(位1的个数)

    题目描述 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量). 示例 : 输入: 11 输出: 3 解释: 整数 11 的二进制表示为 000000 ...

  4. LN : leetcode 191 Number of 1 Bits

    lc 191 Number of 1 Bits 191 Number of 1 Bits Write a function that takes an unsigned integer and ret ...

  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. Java实现 LeetCode 191 位1的个数

    191. 位1的个数 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量). 示例 1: 输入:000000000000000000000000000 ...

  7. LeetCode 191 Number of 1 Bits

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

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

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

随机推荐

  1. gzip压缩及测试方法【转载】

    Nginx开启Gzip压缩大幅提高页面加载速度 http://www.veryhuo.com/a/view/51706.html 刚刚给博客加了一个500px相册插件,lightbox引入了很多js文 ...

  2. mysql开启登录日志和sql日志 排查错误

    首先看是否开启了日志 show global variables like '%general%'; set global general_log = on; // 打开 set global gen ...

  3. 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:5.安装Oracle RAC FAQ-RAC安装DB软件runInstaller看不到节点

    集群安装正常: [root@kmdbrac1 ~]# crs_stat -t -v Name Type R/RA F/FT Target State Host -------------------- ...

  4. RSA加密算法的加密与解密

    转发原文链接:RSA加密算法加密与解密过程解析 1.加密算法概述 加密算法根据内容是否可以还原分为可逆加密和非可逆加密. 可逆加密根据其加密解密是否使用的同一个密钥而可以分为对称加密和非对称加密. 所 ...

  5. CentOS常见问题

    1.图形界面无法启动 检查messagebus服务是否为开机启动:chkconfig --list messagebus 如果状态为不启动,则修改之:chkconfig messagebus on 启 ...

  6. T-SQL中的随机数

    SQL开发中会有生成随机数的需求,下面说几种常用的需求和解决办法(基于MS SQL),最后总结出通用的办法: 1.0-9间的随机整数,包括0和9本身: abs(checksum(newid()))%1 ...

  7. WinForm中使用XML文件存储用户配置及操作本地Config配置文件(zt)

    因项目中采用CS结构读取Web.config文件,故参照一下的连接完成此功能,在此感谢原作者! 原文地址: http://www.cnblogs.com/zfanlong1314/p/3623622. ...

  8. GraphicsMagick+Im4Java在windows和linux下的配置

    GraphicsMagick介绍及安装 Im4Java包为: im4java-1.2.0.jar 直接在lib下引用即可 GraphicsMagick的安装如下: windows下: 安装:Graph ...

  9. Python六大开源框架对比:Web2py略胜一筹

    Python是一门动态.面向对象语言.其最初就是作为一门面向对象语言设计的,并且在后期又加入了一些更高级的特性.除了语言本身的设计目的之外,Python标准库也是值得大家称赞的,Python甚至还自带 ...

  10. Android Studio 小技巧/快捷键 合集

    参考: http://jaeger.itscoder.com/android/2016/02/14/android-studio-tips.html 1. 书签(Bookmarks) 描述:这是一个很 ...