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 ...
随机推荐
- Mongodb for PHP教程之入门安装
简介: MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的.他支持的数据结构非常松散,是类似json的bjson格式,因此可以存储比较复杂的数据 ...
- cocos2d-x CCSrollView 源代码,可循环的SrollView代码
项目须要.写一个类似于iPhone上面时钟选择的可拉动式循环选择列表,通过集成CCScrollView并更改部分代码.实现了该功能. 假设想充分了解代码,请先阅读源码分析http://blog.csd ...
- 基于docker容器搭建fastdfs分布式文件系统
本次环境的搭建参考了 https://blog.csdn.net/qq_43455410/article/details/84797814, 感谢博主. 主要流程如下: 1. 下载fastdfs镜像 ...
- android WIFI信息获取
在androi中WIFI信息的获取能够通过系统提供的WIFI Service获取 [java] WifiManager wifi_service = (WifiManager)getSystemSe ...
- 关于axis2.1.6与websphere7的包冲突问题的解决方式
1,复制axis2.1.6内的module目录内的全部文件到lib 并改动扩展名为.jar 2,删除module目录(可选,不删除也能够) 3,部署到was 4,设置was相应应用程序的类载入方案为父 ...
- oracle多表关联多字段update
多表关联多字段update 有代码有J8: update spatial_references set( auth_name, auth_srid, falsex, falsey, xyunits, ...
- 解决input,number类型的maxlength无效
使用input数字number类型的时候maxlength无效,假设需要控制输入数量为5,可以用以下方式: 无效: <input type="text" maxlength ...
- 在调试状态查看DateTable里的数据信息
- arm位清零bic指令
(1)指令的语法格式 BIC{<cond>}{S} <Rd>,<Rn>,<shifter_operand> BIC(Bit Clear)位清零指令,将寄 ...
- IOS应用在iPhone5和iPhone5s上不能全屏显示,应用画面上下各有1条黑色的解决方案
设置启动图片就可以了: 添加启动图: 接着设置为启动图: 这样就ok了