leetcode:Number of 1 Bits
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.
分析:题意为计算32位整型数2进制表示中1的个数。
方法还是非常多的,也很好理解:
1、利用2进制的数学特点来
class Solution {
public:
int hammingWeight(uint32_t n) {
int count=0;
while(n){
if(n%2==1) count++;
n=n/2;
}
return count;
}
};
2、将每一位分别和1做与运算,计算不为0的个数即可
class Solution {
public:
int hammingWeight(uint32_t n) {
int count=0;
while(n){
count+=n&1;
n>>=1;
}
return count;
}
};
3、每次n&(n-1)可以将n里面的值为1的位数减少一位
class Solution {
public:
int hammingWeight(uint32_t n) {
int sum = 0;
while (n)
{
n &= (n-1);
++sum;
}
return sum;
}
};
leetcode:Number of 1 Bits的更多相关文章
- [LeetCode] Prime Number of Set Bits in Binary Representation 二进制表示中的非零位个数为质数
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...
- [LeetCode] Binary Number with Alternating Bits 有交替位的二进制数
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...
- LeetCode OJ:Number of 1 Bits(比特1的位数)
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- LeetCode 191:number of one bits
题目就是: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (als ...
- LeetCode 191. Number of 1 bits (位1的数量)
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- [LeetCode] 191. Number of 1 Bits 二进制数1的个数
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- 【leetcode】Number of 1 Bits
题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...
- LeetCode 191 Number of 1 Bits
Problem: Write a function that takes an unsigned integer and returns the number of '1' bits it has ( ...
- Java for LeetCode 191 Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
随机推荐
- PE文件信息获取工具-PEINFO
能实现基本的信息获取 区段信息 数据目录信息 导入表函数分析 导出表函数分析,能同时解析只序号导出和以函数名序号同时导出的函数 FLC计算 需要源码的可以留邮箱.
- try-catch语句讲解
try-catch 语句由一个 try 块后跟一个或多个 catch 子句构成,这些子句指定不同的异常处理程序. 引发异常时,公共语言运行时 (CLR) 会查找处理此异常的 catch 语句. 如果当 ...
- SQLserver分页查询实例
Sqlserver数据库分页查询一直是Sqlserver的短板,闲来无事,想出几种方法,假设有表ARTICLE,字段ID.YEAR...(其他省略),数据53210条(客户真实数据,量不大),分页查询 ...
- sql server 2008 执行计划
SSMS允许我们查看一个图形化的执行计划(快捷键Ctrl+L)
- 企业级Java应用最重要的4个性能指标
应用性能管理(APM)是一种即时监控以实现对应用程序性能管理和故障管理的系统化解决方案.目前主要指对企业的关键业务应用进行监测.优化,最终达到提高企业应用的可靠性和质量,保证用户得到良好的服务,降低I ...
- iOS搜索栏
百度云连接:http://pan.baidu.com/s/1pJLzDFX
- sream bytes[] img相互转换
/// <summary> /// 将 Stream 转成 byte[] /// </summary> /// <param name="stream" ...
- OPEN资讯
http://www.open-open.com/news/view/1f55540 随着 Android 平台市场份额的持续猛增 , 越来越多的开发者开始投入 Android 应用程序的开发大潮.如 ...
- IIS 发布程序的一些心得
1.应用程序池一般自己建立对应Framework版本的程序池,并托管管道模式为经典 2.在IIS根目录双击,右侧的“ISAPI和CGI限制” 双击打开,将自己所需要的Framework版本的限制设置为 ...
- Win7 _Object_header 中的 TypeIndex
Win7 比较 xp下ObjectHeader中的内容有所变化,xp直接在OBJECT_HEADER里保存了POBJECT_TYPE指针,而Win7中把所有的对象类型放在了一个表里,这个表叫做ObTy ...