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 ...
随机推荐
- [工作积累] jboolean is neither JNI_TRUE nor JNI_FALSE
jboolean result = env->CallBooleanMethod(ShopDataAndroid.IAPBridge_Object, ShopDataAndroid.IAPBri ...
- [百度空间] [转] 在 Visual C++ 中控制全局对象的初始化顺序
from: http://blog.csdn.net/classfactory/archive/2004/08/07/68202.aspx 在 C++ 中,同一个翻译单位(.cpp文件)里的全局对象的 ...
- KMP_Best Reward
大意:把一个字符串分成两串,假如一个字符串是回文串就可以加上它的VALUE,否则它的VALUE为0: KMP的特点是可以求出前缀与后面的字符串是否匹配, 注意回文串的特点,所以当我们把回文串反转的时候 ...
- iOS导航栏-导航栏透明
设置一张透明图片:nav_bargound.png //导航栏背景 [self.navigationController.navigationBar setBackgroundImage:[ ...
- JavaWeb-JDK下载安装
JDK官方下载地址:http://www.oracle.com/index.html JDK下载: 64位的下64的 JDK安装:(这是32位的) JDK部署测试:(配置环境变量) JAVA_HOME ...
- D&F学数据结构系列——前驱和后继
前驱和后继 本文所述为二叉排序树的前驱和后继,如果想了解二叉排序树的概念,可以参考我的博文http://www.cnblogs.com/sage-blog/p/3864640.html 给定一个二叉查 ...
- JavaScript基础(一)
我是一个初学者,但求能学到些许知识!以下是根据韩顺平老师的<轻松搞定网页设计html+css+javascript—javascrip部分>整理而成. 为什么要学习javascript? ...
- 在Jmeter中使用自定义编写的Java测试代码
我们在做性能测试时,有时需要自己编写测试脚本,很多测试工具都支持自定义编写测试脚本,比如LoadRunner就有很多自定义脚本的协议,比如"C Vuser","Java ...
- RHadoop计算平台搭建
原创文章,转载请注明: 转载自www.cnblogs.com/tovin/p/3824554.html 本文基于CentOS6.4系统介绍基于RHadoop平台的搭建,Hadoop的搭建可以参考ht ...
- <iostream> 和 <iostream.h>的区别 及 Linux下编译iostream.h的方法
0.序言 其实2者主要的区别就是iostream是C++标准的输入输出流头文件,而iostream.h是非标准的头文件. 标准头文件iostream中的函数属于标准命令空间,而iostream.h中的 ...