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.

Credits:

Special thanks to @ts for adding this problem and creating all test cases.

This problem can be done in log(n) time, need >> operations..

class Solution:
# @param n, an integer
# @return an integer
def hammingWeight(self, n):
res=0
while n>0:
res+=n&1
n>>=1
return res

版权声明:本文博主原创文章,博客,未经同意不得转载。

191. Number of 1 Bits Leetcode Python的更多相关文章

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

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

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

  3. 【LeetCode】191. Number of 1 Bits 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 右移32次 计算末尾的1的个数 转成二进制统计1的个 ...

  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 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. 【一天一道LeetCode】#191. Number of 1 Bits

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...

  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. selenium让人摸不着头脑的问题

    selenium让人摸不着头脑的问题 问题一 使用webdriver驱动firefox浏览器时如果不设置参数,默认使用的Firefox的profile和平时打开浏览器使用的firefox不一样,如果要 ...

  2. ExtJs 设置GridPanel表格文本垂直居中

    业务场景,需要实现最终效果图如下: GridPanel代码如下配置: { xtype : 'grid', id : 'grid_jglb', frame : true, region : 'cente ...

  3. git配置流程

    写的比较简略,主要是记录一下,以后配置别的机器的时候看一下,仅供参考. 1 安装 命令依赖包 sudo apt-get install git-core git-gui git-doc 2 设置SSH ...

  4. Mysql怎样删除以“#sql-”开头的暂时表

    author:skate time:2014/09/28 Mysql怎样删除以"#sql-"开头的暂时表 现象:在重建索引后,发现Mysqlserver的磁盘空间快满了 在用例如以 ...

  5. sql语句查询数据库中的表名/列名/主键/自动增长值

    原文地址:http://blog.csdn.net/pukuimin1226/article/details/7687538 ----查询数据库中用户创建的表 ----jsj01 为数据库名 sele ...

  6. 核心ASP.NET

    1.用于Web应用程序的.NET Framework ASP.NET是.NET Framework的一部分,它可以在通过HTTP请求文档是在WEB服务器上动态创建它们,在.Net Framework中 ...

  7. POJ 1838 Banana (并查集)

    Description Consider a tropical forrest, represented as a matrix. The cell from the right top corner ...

  8. 利用 C++ 单向链表实现队列

    利用C++ 单向链表实现数据结构队列,其实和上一篇基本内容相同,仅仅是插入的时候在链表的尾部插入,取元素都是一样的,都从头部取. #pragma once #include "stdio.h ...

  9. 全栈project师的毁与誉

    全栈(Full Stack)project师.也能够叫全端project师,不管是前端知识,还是后端架构你都要了解.甚至有些调皮的程序猿这样理解全栈project师:全栈project师 = 屌丝战斗 ...

  10. QT Programming 1

    1.控制台输出 helloworld #include<QtCore/QCoreApplication> #include<QDebug> int main(int argc, ...