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

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

最新文章

  1. iOS7中如何去除UINavigationbar下边的那条黑线
  2. iOS开发--UITableView
  3. php curl 的几个实例
  4. BotVS趋势交易策略-MACD
  5. js基础——运算符
  6. HTML知识点总结之img、scirpt、link标签
  7. Protobuf 从入门到实战
  8. hadoop hdfs ha 模式
  9. python基础2 数据类型、数据拼接、数据转换
  10. 转:敏捷开发之Scrum扫盲篇
  11. 同步手绘板——android端取色
  12. 20155117王震宇 2016-2017-2 《Java程序设计》第六周学习总结
  13. Lerning Entity Framework 6 ------ Using a commandInterceptor
  14. 微信小程序之蓝牙开发(详细读数据、写数据、附源码)
  15. 软工网络15团队作业8——Beta阶段敏捷冲刺(Day4)
  16. New Concept English Two 20 52
  17. oracle/ms sql 系统表
  18. Java生成验证码简记
  19. Python-PyQt4学习资料汇总
  20. OpenCV---resize

热门文章

  1. 《转》VC++多线程编程
  2. OCP读书笔记(1) - Oracle核心概念和工具
  3. 陈词滥调,正确使用memset
  4. Docker 用法总结之:管理工具 shipyard 的具体使用指南
  5. Software Development and Newton's Laws of Motion
  6. hdu 1849 (尼姆博弈)
  7. SPARK如何使用AKKA实现进程、节点通信
  8. WPF界面设计技巧(11)-认知流文档 & 小议WPF的野心
  9. Maven插件之buildnumber-maven-plugin
  10. 分布式Unique ID的生成方法