191. Number of 1 Bits Leetcode Python
2015-10-03 14:08
579 查看
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
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..
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
相关文章推荐
- 使用urllib编写python爬虫
- Python Tab/space error
- python连接MySQL数据库
- python sorted函数
- python 运算符 重载可使用
- Python 新浪微博中提取最常见转载的微博转载了几次,username,内容
- 八大排序算法的 Python 实现
- LeetCode----Implement Trie (Prefix Tree)
- Unix时间戳转换(python)
- Python安全编码与代码审计
- Python语法基础_控制流语句_if、while、for
- 试用Python+Psycopg操作PostgreSQL
- Python学习笔记<LearnPythonHardWay>
- Python学习笔记<LearnPythonHardWay>
- 1.Python深入_特殊方法与多范式
- 在win7下安装python开发环境和numpy,scipy,matplotlib模块的问题
- IPython :一个交互式计算和开发环境
- Anaconda python各模块简介
- [python]逆水行舟不进则退(1)
- 9.Python进阶_动态类型