191. Number of 1 Bits
2015-12-23 11:39
246 查看
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.
class Solution(object):
def hammingWeight(self, n):
"""
:type n: int
:rtype: int
"""
c=0
while n:
c+=1
n=(n-1)&n
return c
weight).
For example, the 32-bit integer ’11' has binary representation
00000000000000000000000000001011,
so the function should return 3.
class Solution(object):
def hammingWeight(self, n):
"""
:type n: int
:rtype: int
"""
c=0
while n:
c+=1
n=(n-1)&n
return c
相关文章推荐
- handler 常用方法总结
- Android 方向传感器的简单使用
- 重写与重载(区别与用途)
- Imageview另类切换图片的思路
- 2-1-单链表顺序存储结构-线性表-第2章-《数据结构》课本源码-严蔚敏吴伟民版
- linux的hostname修改详解
- 【转载】断点调试
- iOS 检测版本更新
- 为文本的不同部分添加不同的背景色
- nginx主要配置
- PHP清除HTML代码、空格、回车换行符的函数
- 从apache mod_php到php-fpm
- MFC VS 常见错误
- 手把手教你使用Git
- 236. Lowest Common Ancestor of a Binary Tree
- android studio 使用问题2
- [转]EXCEL如何使用动态公式
- 如何让生命中不喜欢的人一笑而过
- FFMPEG SDK开发介绍(转)
- Python序列的方法(转)