题目:

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. Loading加载小插件,用户可以选择html格式或canvas格式,自定义loading图片,canvas色彩搭配可根据个人喜好

    ;(function($) { $.Loading = function(options) { //暴漏插件默认值 $.Loading.defaults = { speed: 200, //弹出层淡出 ...

  2. mongo 相关命令

    mongo导入数据: 1. 先进入找到mongo 安装目录 执行 ./mongo 进入mongo 2. mongorestore -u 用户名 -p 密码 -d 数据库 —drop 文件存在路径 显示 ...

  3. url的内容及格式

    url的内容及结构: url格式:

  4. 理解Javascript之执行上下文(Execution Context)

    1>什么是执行上下文 Javascript中代码的运行环境分为以下三种: 全局级别的代码 - 这个是默认的代码运行环境,一旦代码被载入,引擎最先进入的就是这个环境. 函数级别的代码 - 当执行一 ...

  5. web sql database数据存储位置

    Q1: 数据存储在哪儿? Web Storage / Web SQL Database / Indexed Database 的数据都存储在浏览器对应的用户配置文件目录(user profile di ...

  6. 学习PYTHON之路, DAY 3 - PYTHON 基础 3 (函数)

    一 set 集合 (无序且不重复的元素集合) 基本操作: t.add('x') # 添加一项 s.update([10,37,42]) # 在s中添加多项 删除一项: t.discard('H') t ...

  7. jquery中隐藏div的几种方法

    //jQuery中的显示.隐藏方法 $("#id").show()://表示display:block,    $("#id").hide()://表示disp ...

  8. jmeter

    Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试但后来扩展到其他测试领域. 它可以用于测试静态和动态资源例如静态文件. ...

  9. My Game --文件读取数据

    My Game --线段数据 中说到背景的绘制由贝赛尔曲线生成线段,用 DrawNode 画多边形,同时一张背景有两座山,一座山有两条以上贝赛尔曲线保存,用了嵌套的数据类:Bezier,LineLay ...

  10. python网络编程【一】

    TCP/IP 是标准的协议,它可以使用世界范围内的计算机通过Internet或本地的网络通信 1.编写一个TCP客户端程序 #!/usr/bin/env python import socket, s ...