题目:

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.

实现:

 class Solution {
public:
int hammingWeight(uint32_t n) {
int count = ;
while(n)
{
count++;
n = n & (n - );
}
return count;
}
};

Number of 1 Bits(Difficulty: Easy)的更多相关文章

  1. LeetCode算法题-Binary Number with Alternating Bits(Java实现)

    这是悦乐书的第292次更新,第310篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第160题(顺位题号是693).给定正整数,检查它是否具有交替位:即它的二进制数的任意两 ...

  2. LeetCode算法题-Number of 1 Bits(Java实现)

    这是悦乐书的第186次更新,第188篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第45题(顺位题号是191).编写一个带无符号整数的函数,并返回它所具有的"1 ...

  3. Counting Bits(Difficulty: Medium)

    题目: Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate ...

  4. Power of Four(Difficulty: Easy)

    题目: Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example ...

  5. Integer Break(Difficulty: Easy)

    题目: Given a positive integer n, break it into the sum of at least two positive integers and maximize ...

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

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

  7. hdu 4670 Cube number on a tree(点分治)

    Cube number on a tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/ ...

  8. A1082 Read Number in Chinese (25)(25 分)

    A1082 Read Number in Chinese (25)(25 分) Given an integer with no more than 9 digits, you are suppose ...

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

随机推荐

  1. Caché数据库学习笔记(5)

    目录 Cache数据库方法的RESTful封装 ================================================================ 因为对web serv ...

  2. 正则匹配中 ^ $ 和 \b 的区别

    正则匹配中 ^ $ 和 \b 的区别     ^和$分别代表字符串的开始和结束,因此^\d$只能匹配包含一个数字的字符串\b代表单词边界,其前后必须是不同类型的字符,可以组成单词的字符为一种类型,不可 ...

  3. eclipse不显示Android SDK Manager标签

    新版的eclipse配置好android开发环境后没有显示在window菜单里显示Android SDK Manager,也没有在工具栏里出现android的工具图标.但可以通过android sdk ...

  4. Oculus安装问题

    1.必须FQ,可采用蓝灯,或其他vpn 2.Your computer doesn't meet Rift's recommended specifications 如果某些硬件达不到推荐配置(比如我 ...

  5. web自学网站

    coursera 网站很多新的技术,都是大牛和牛大学的,和外国新技术.   我要自学网 有好也有不怎么样的,一般般的.但是dome多.   中国教程网  一般.   CSDN   技术相关    站长 ...

  6. MySQL数据库的备份与还原

    http://www.cnblogs.com/lql123/p/6090681.html    //安装WordPress 1.备份 密码为:AAAzzz//123 mysqldump -uroot ...

  7. uva 820(最大流)

    最大流的裸题,直接贴了模板. #include <cstdio> #include <iostream> #include <sstream> #include & ...

  8. Mindmanager安装

  9. [原创]C#引用C++编译的dll

    一.DllImportAttribute 1.属性介绍 using System; using System.Reflection; using System.Security; namespace ...

  10. composer 一些使用说明

    1 使用订制的包 配置 "repositories": [ { "type": "path", "url": " ...