191 Number of 1 Bits 位1的个数
编写一个函数,输入是一个无符号整数,返回的是它所有 位1 的个数(也被称为汉明重量)。
例如,32位整数 '11' 的二进制表示为 00000000000000000000000000001011,所以函数返回3。
详见:https://leetcode.com/problems/number-of-1-bits/description/
Java实现:
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int cnt=0;
while(n!=0){
n=(n-1)&n;
++cnt;
}
return cnt;
}
}
C++实现:
方法一:
class Solution {
public:
int hammingWeight(uint32_t n) {
int cnt=0;
unsigned int flag=1;
while(flag)
{
if(flag&n)
{
++cnt;
}
flag<<=1;
}
return cnt;
}
};
方法二:
class Solution {
public:
int hammingWeight(uint32_t n) {
int cnt=0;
while(n)
{
n=(n-1)&n;
++cnt;
}
return cnt;
}
};
191 Number of 1 Bits 位1的个数的更多相关文章
- [LeetCode] 191. Number of 1 Bits ☆(位 1 的个数)
描述 Write a function that takes an unsigned integer and return the number of '1' bits it has (also kn ...
- 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 ...
- [LeetCode] 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#191. Number of 1 Bits(位1的个数)
题目描述 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量). 示例 : 输入: 11 输出: 3 解释: 整数 11 的二进制表示为 000000 ...
- LN : leetcode 191 Number of 1 Bits
lc 191 Number of 1 Bits 191 Number of 1 Bits Write a function that takes an unsigned integer and ret ...
- 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
Problem: Write a function that takes an unsigned integer and returns the number of '1' bits it has ( ...
- 191. Number of 1 Bits
题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...
随机推荐
- VC++中的int main(int argc, char argv[])是什么意思
这是C/C++的一重要函数,叫主函数.无论程序多复杂,代码中必须有这么一个函数,也只能有一个这样的函数:程序执行时就是从这个函数进入的.由于问得比较笼统,如果你想知道详细情况的话,发给你一个网友的求助 ...
- POJ-2240 -Arbitrage(Bellman)
题目链接:Arbitrage 让这题坑了,精度损失的厉害.用赋值的话.直接所有变成0.00了,无奈下,我仅仅好往里输了,和POJ1860一样找正环,代码也差点儿相同,略微改改就能够了,可是这个题精度损 ...
- web 界面设计---大道至简
http://www.cnblogs.com/coder2012/p/4023442.html 一个非常精简的webpy页面博客 qing.weibo.com 新浪的轻微博也不错精简
- SpringMVC_中文乱码的配置 --跟海涛学SpringMVC(和自己在项目中的实际使用的对比)
spring Web MVC框架提供了org.springframework.web.filter.CharacterEncodingFilter用于解决POST方式造成的中文乱码 <filte ...
- 《CMake实践》笔记二:INSTALL/CMAKE_INSTALL_PREFIX【转】
本文转载自:http://www.cnblogs.com/52php/p/5681751.html 四.更好一点的Hello World 没有最好,只有更好 从本小节开始,后面所有的构建我们都将采用 ...
- ToolBar修改返回按钮图标
使用Toolbar时,有时因为不同的手机设备,不能使用系统默认的主题样式或者图标,必须指定特定的资源,防止APP在不同设备上的效果不一样! 我在使用Toolbar时,把这个布局作为一个公共的了,所以修 ...
- 获取Android APK JNI库
/************************************************************************** * 获取Android APK JNI库 * 说 ...
- AutoIT: 对Windows桌面的一些操作
$handle= WinGetHandle("Program Manager") $ctrl= ControlGetHandle("ProgramManager" ...
- python的md5和base64加密
在用jmeter测试接口时,有的请求参数会加密,例如,回流接口:http://ip:port/oms-gateway-datareflow-mq/orderReflow/tmsPracticeActi ...
- 377D
树形dp 就是求每个点到标记的点的最大距离 一个经典模型 有一个巧妙的方法,我们求出以这些关键点的直径,然后每个点到关键点的最大距离就是到直径两端的距离 #include<bits/stdc++ ...