[leedcode 191] Number of 1 Bits
2015-08-03 20:34
495 查看
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
For example, the 32-bit integer ’11' has binary representation
00000000000000000000000000001011, so the function should return 3.
public class Solution { // you need to treat n as an unsigned value public int hammingWeight(int n) { int res=0; while(n!=0){ n=n&(n-1); res++; } return res; } }
相关文章推荐
- Target-Action(View实现button的效果)
- HDU1.1.1 A+B for Input-Output Practice (I)
- CentOS7安装redis数据库及php-redis扩展
- 图的环路问题
- 类的注册与动态生成
- 遍历集合
- 如何理解挂载点
- sql 基础
- hdu 3996 Gold Mine 最大权闭合子图
- NodeJS 常用模块整理
- linux将程序放到后台执行
- 数据结构与算法分析 java语言描述
- [hdu1079]简单博弈
- STL之queue
- [leedcode 190] Reverse Bits
- 最小费用最大流:HDU3488
- 指针合并字符串
- Java基础知识 第三章 流程控制
- linux的mount(挂载)命令详解
- Install And Configure Nagios on Ubuntu 14.04 LTS