191. Number of 1 Bits
2016-04-12 18:14
267 查看
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.
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 re = 0; while(0 != n) { n = n&(n - 1); ++re; } return re; } }
相关文章推荐
- AngularJS for beginners
- Mongodb 字段类型转换
- 怎样解决VirtualBox的Unable to load R3 module
- 第一篇博客
- 【tar】只归档不压缩
- div的高度自适用图片的高度
- IOS使用ijkPlayer
- 安装Puppet
- 对于初学iOS开发者有用之在UITableView的导航栏中加入Item的方法
- resion 虚拟目录、虚拟主机配置
- HDU 4006 The kth great number(线段树【亚洲区网络赛题目】)
- 利用jena读取owl文件的父子属性以及其他属性
- @Autowired的使用
- Unity5.x在WP8.1中无法使用Reflection API的解决方法
- java RMI入门指南
- wingIDE5.1中文乱码解决
- C# IndexOf、LastIndexOf、IndexOfAny,LastIndexOfAny
- plsqldev中查看表结构
- Objective-C修改cell左滑按钮背景色
- 数据结构之链表