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. [THINKING IN JAVA]操作符

    3 操作符 3.1 static import 可以static import静态方法和变量,这样就可以在不写类名的前提下调用类中的静态方法和变量: package com.chenlei.study ...

  2. POJ题目排序的Java程序

    POJ 排序的思想就是根据选取范围的题目的totalSubmittedNumber和totalAcceptedNumber计算一个avgAcceptRate. 每一道题都有一个value,value ...

  3. 使用"立即执行函数"(Immediately-Invoked Function Expression,IIFE)

    一.原始写法 模块就是实现特定功能的一组方法. 只要把不同的函数(以及记录状态的变量)简单地放在一起,就算是一个模块. function m1(){ //... } function m2(){ // ...

  4. Hibernate的save()和persist()的区别

    hibernate之所以提供与save()功能几乎完全类似的persist()方法,一方面是为了照顾JPA的用法习惯.另一方面,save()和 persist()方法还有一个区别:使用 save() ...

  5. 为了方便可灌入自定义方法AppendLog 比如File

    说起到qt的编译,真是领人痛心啊,不仅编译选项繁多,而且编译时间比较久,总是能使想编译qt源码的人望而却步,呵呵...我就是其中一个,不知道从什么时候开始就想着把qt的源码编译一下,也尝试过几次,但都 ...

  6. IOS-指定返回Modal的控制器presentViewController

    [self.presentingViewController.presentingViewController dismissViewControllerAnimated:NO completion: ...

  7. Ubuntu 16.04中安装Chromium浏览器

    引用:http://www.linuxidc.com/Linux/2016-05/131097.htm 通过ppa,安装最新版本的Chromium浏览器 sudo add-apt-repository ...

  8. PHP空数组转化为json对象的问题

    例子: $a = []; echo json_encode($a); echo json_encode($a, JSON_FORCE_OBJECT); 输出结果: [] {}

  9. 在Eclipse中使用JUnit4进行单元测试(高级篇)

    通过前2篇文章,您一定对JUnit有了一个基本的了解,下面我们来探讨一下JUnit4中一些高级特性. 一.高级Fixture 上一篇文章中我们介绍了两个Fixture标注,分别是@Before和@Af ...

  10. varnish 4.0.3 域名访问的小问题

    1,若端口不是80 端口则匹配的时候必须加端口 if (req.http.host ~ "(?i)^var.test.aa:6081$") {set req.http.host = ...