LeetCode 191 Number of 1 Bits
2016-02-04 17:28
417 查看
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as theHamming weight).
For example, the 32-bit integer ’11' has binary representation
求32位无符号整数二进制里1的个数
8ms
For example, the 32-bit integer ’11' has binary representation
00000000000000000000000000001011, so the function should return 3.
求32位无符号整数二进制里1的个数
8ms
class Solution { public: int hammingWeight(uint32_t n) { int cnt=0; while(n) { cnt+=(n&1)?1:0; n>>=1; }; return cnt; } };
相关文章推荐
- 第十八章 并发登录人数控制——《跟我学Shiro》
- Spring学习3—控制反转(IOC)基于Annotation(注解)的依赖注入实现
- ios关于如何获取手机当前的ip
- 学习笔记------数据结构(C语言版)栈和递归 汉诺塔
- eclipse加速之禁用JS、jsp等文件的语法验证,eclipsejs
- SQL Server Export and Import 导出和导入工具 导入EXCEL居然限制为255列
- nyoj264国王的魔镜
- Spring学习3—控制反转(IOC)Spring依赖注入(DI)和控制反转(IOC)
- Spring学习2—Spring容器
- Android的WebView中js与java代码相互调用
- hdu 4289(最小割最大流定理)
- 导出为Excel例子 java
- HUE中通过SQL进行数据分析
- Sublime Text 3使用技巧
- getContextPath、getServletPath、getRequestURI的区别
- Spring学习1-初识Spring
- N皇后问题
- 3D Touch介绍: 一个数字压力器App和Quick Actions
- weblogic 10.x 上开发restful服务
- ReactNative之在iOS项目中手动加入ReactNative