191. Number of 1 Bits
2016-03-18 20:57
211 查看
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
so the function should return 3.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
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.
Subscribe to see which companies asked this question
public class Solution { // you need to treat n as an unsigned value public int hammingWeight(int n) { int count = 0; while(n!=0){ n = n&(n - 1); ++count; } return count; } }
相关文章推荐
- Hadoop 1.x 实现文件上传合并功能
- 【bzoj 3190】: [JLOI2013]赛车 暴力
- 栈的应用:表达式求值运算
- MySQL Cluster 备份与恢复
- K近邻分类算法实现 in Python
- 235. Lowest Common Ancestor of a Binary Search Tree
- HDOJ 2013 蟠桃记
- 3.嵌入式开发模式详解
- C#生成图片验证码
- GET和POST请求
- python字符串操作2
- Activity
- 13. Roman to Integer
- 第一个SQL表格开发代码-20160318
- ue4 常见问题解答
- 深入分析AIDL原理
- HDU 1016 Prime Ring Problem(搜索入门题 DFS,多种方式)
- 面向对象编程(六)抽象类
- 206. Reverse Linked List
- Java环境变量的配置