leetcode 191
191. 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位中有多少位值为1.
代码如下:
class Solution {
public:
int hammingWeight(uint32_t n) {
unsigned pos = << ;
int count = ;
while(pos)
{
if(pos & n)
{
count++;
}
pos >>= ;
}
return count;
}
};
leetcode 191的更多相关文章
- C#版 - Leetcode 191. Number of 1 Bits-题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- 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 ...
- Java实现 LeetCode 191 位1的个数
191. 位1的个数 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量). 示例 1: 输入:000000000000000000000000000 ...
- 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 ...
- (easy)LeetCode 191.Number of 1 Bits
Number of 1 Bits Write a function that takes an unsigned integer and returns the number of ’1' bits ...
随机推荐
- [BZOJ 2819]Nim
最近都忙的没空写题解了喵- 看到 1= 终于是保住了也算是一个小小的安慰吧 555…… 湖北省队互测题,据说会爆栈,但 Linux 下 栈空间=内存=128M 真的吃不下? 反正我是写了个人工栈- 这 ...
- 如何将自己的windows设置为mysql服务器
1.安装mysql 服务器 2.创建超级用户,即 用户管理 mysql>use mysql; 查看 mysql> select host,user,password from user ; ...
- 使用keepalived实现mysql主从复制的自动切换
最近测试了一下mysql+keepalived实现主从自动切换,主从都需要安装keepalived,使用vip漂移实现主从自动切换,这里主要记录的是keepalived的文件配置. 这里mysql搭建 ...
- HA简介以及HBase简介
HBase基础知识: 一,HMater节点:可以启动多个HMaster,通过Zookeeper的Master Election机制保证总有一个Master运行 1,为Region server 分配r ...
- 兼容性js中setTimeout 传参“保值”方案
这里所谓“保值”,是指在setTimeout中指定的时间后,执行指定的方法所用到的“参数”值,跟执行setTimeout时该“参数”值一样.是不是有点懵?看如下例子: ================ ...
- ruby发送邮件方法
#encoding:utf-8require 'mail'def send_email sum,fail,case_path,name,receive smtp = { :address => ...
- How to backup your blogs on cnblogs
This is an alternative to OfflineExplorer. Thanks for this article[1] which is listed in Reference s ...
- BFS与DFS
DFS:使用栈保存未被检测的结点,结点按照深度优先的次序被访问并依次被压入栈中,并以相反的次序出栈进行新的检测. 类似于树的先根遍历深搜例子:走迷宫,你没有办法用分身术来站在每个走过的位置.不撞南山不 ...
- 拥抱高效、拥抱 Bugtags 之来自用户的声音 2
小编按:这是一篇 Bugtags 用户来稿,主要是介绍了使用 Bugtags 前后对测试及解决 Bug 所带来的变化,感谢单车娱乐 App 工程师 - 李斌同学对 Bugtags 的信赖和支持.小编在 ...
- lucene 内存索引 和文件索引 合并
IndexWriter.addIndexes(ramDirectory); http://blog.csdn.net/qq_28042463/article/details/51538283 在luc ...