191. Number of 1 Bits Leetcode Python
2024-08-27 04:10:42
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
版权声明:本文博主原创文章,博客,未经同意不得转载。
最新文章
- iOS7中如何去除UINavigationbar下边的那条黑线
- iOS开发--UITableView
- php curl 的几个实例
- BotVS趋势交易策略-MACD
- js基础——运算符
- HTML知识点总结之img、scirpt、link标签
- Protobuf 从入门到实战
- hadoop hdfs ha 模式
- python基础2 数据类型、数据拼接、数据转换
- 转:敏捷开发之Scrum扫盲篇
- 同步手绘板——android端取色
- 20155117王震宇 2016-2017-2 《Java程序设计》第六周学习总结
- Lerning Entity Framework 6 ------ Using a commandInterceptor
- 微信小程序之蓝牙开发(详细读数据、写数据、附源码)
- 软工网络15团队作业8——Beta阶段敏捷冲刺(Day4)
- New Concept English Two 20 52
- oracle/ms sql 系统表
- Java生成验证码简记
- Python-PyQt4学习资料汇总
- OpenCV---resize
热门文章
- 《转》VC++多线程编程
- OCP读书笔记(1) - Oracle核心概念和工具
- 陈词滥调,正确使用memset
- Docker 用法总结之:管理工具 shipyard 的具体使用指南
- Software Development and Newton&;#39;s Laws of Motion
- hdu 1849 (尼姆博弈)
- SPARK如何使用AKKA实现进程、节点通信
- WPF界面设计技巧(11)-认知流文档 &; 小议WPF的野心
- Maven插件之buildnumber-maven-plugin
- 分布式Unique ID的生成方法