191. Number of 1 Bits Leetcode Python
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的更多相关文章
- Leetcode#191. Number of 1 Bits(位1的个数)
题目描述 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量). 示例 : 输入: 11 输出: 3 解释: 整数 11 的二进制表示为 000000 ...
- 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 ...
- 【LeetCode】191. Number of 1 Bits 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 右移32次 计算末尾的1的个数 转成二进制统计1的个 ...
- [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 ...
- 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 ...
- 【一天一道LeetCode】#191. Number of 1 Bits
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
- LeetCode 191 Number of 1 Bits
Problem: Write a function that takes an unsigned integer and returns the number of '1' bits it has ( ...
- 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 ...
- (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 ...
随机推荐
- opencv 边缘羽化,边缘过渡
原地址:http://blog.csdn.net/sogarme/article/details/12942971 当把前景和背景分开时,黑色代表背景,白色代表前景,如下图1—记作img1 为了平滑过 ...
- nginx 区分pc和mobile 到不同的404页面
if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry|vivo)') { set $mobile_request '1'; } ...
- [cocos2d-x][apk打包][Fatal signal 11][andriod]Eclipse编译Fatal signal 11报错-都是字符赋值惹的祸
流程重现: 使用coco2d-x制作了一个2048,在xcode模拟器执行以及在pad上真机调试都是没有问题的. 可是在使用eclipse调试打包android可以执行,可是进入游戏之后会在随机的地方 ...
- UDP vs. TCP
UDP vs. TCP 原文:UDP vs. TCP,作者是Glenn Fiedler,专注于游戏网络编程相关工作多年. 说在最前面的话 翻译这篇文章的初衷:我在工作中根本接触不到网络游戏编程,但是我 ...
- 使用和制作patch文件
使用和制作patch文件 发表时间: 2007-2-13 20:57 作者: superuser 来源: 迷茫人 字体: 小 中 大 | 打印 原文http://www.linuxsir. ...
- zend framework将zip格式的压缩文件导入并解压到指定文件
html代码 <pre class="php" name="code"><fieldset> <legend>批量导入学生照 ...
- Xenu-web开发死链接检測工具应用
Xenu 是一款深受业界好评,并被广泛使用的死链接检測工具. 时常检測站点并排除死链接,对站点的SEO 很重要,由于大量死链接存在会减少用户和搜索引擎对站点的信任,web程序开发者还可通过其找到死链接 ...
- SVN更改登录用户(转)
一) 原地址:http://www.ixna.net/articles/2606 //证书缓存 $ svn list https://host.example.com/repos/project Er ...
- MySQLdb 连接Mysql 数据库出错解决
#coding=utf-8 import MySQLdb if __name__ == "__main__": db = MySQLdb.connect(host=<sp ...
- Compass用法指南
Compass用法指南 Sass是一种"CSS预处理器",可以让CSS的开发变得简单和可维护.但是,只有搭配Compass,它才能显出真正的威力. 本文介绍Compass的用法 ...