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.

class Solution {
public:
int hammingWeight(uint32_t n) {
int ret = ;
for (unsigned int i = ; i != ; i <<= ){
if(n & ) ret++;
n >>= ;
}
return ret;
}
};

191. Number of 1 Bits (Int; Bit)的更多相关文章

  1. Leetcode#191. Number of 1 Bits(位1的个数)

    题目描述 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量). 示例 : 输入: 11 输出: 3 解释: 整数 11 的二进制表示为 000000 ...

  2. 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 ...

  3. LeetCode 191 Number of 1 Bits

    Problem: Write a function that takes an unsigned integer and returns the number of '1' bits it has ( ...

  4. 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 ...

  5. (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 ...

  6. Java [Leetcode 191]Number of 1 Bits

    题目描述: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (als ...

  7. 191. Number of 1 Bits

    题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...

  8. 【LeetCode】191. Number of 1 Bits

    题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...

  9. 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 ...

随机推荐

  1. html _ 提取html片段内的纯文本

    var html = “html字符串”;var textstr =html.replace(/<[^>]*>|/g,"");//纯文本

  2. js中实现cookie的增删改查(document.cookie的使用详情)

    一.设置cookie的值 1.每个cookie都是一个名称/值对,名称/值对用等号连接,并将该名称/值对赋值给document.cookie即可.如:document.cookie="id= ...

  3. js Json数组的增删改查

    <!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...

  4. python实现最大重叠子串的查找

    #!/usr/bin/python #查找最大重叠子串 def FindMaxDup(in_str): str_len = len(in_str) result = '' #逐级扩大搜索长度# lev ...

  5. Array.Resize(ref arry, size);

    数组原来的内容不变,后面添加新的空间. 内部操作应该是:重新分配了一块空间,然后将旧的内容拷过去

  6. NGUI 背景图自适应

    背景图UISprite组件调整如下: UIRoot设置: 不保持比例自适应: 保持宽与屏幕宽一致,高度随宽的缩放比例进行缩放:

  7. jenkins+docker+docker-compose完整发版流程

    首先搭建jenkins+maven+nexus这一套自动化打包工具,并配置好相应配置,这里就不再赘述了. 其次,搭建好docker集群和私有仓库,以及安装好docker-compose工具,配置好相应 ...

  8. linux初始化

    [Linux 系统启动过程] Linux的启动其实和windows的启动过程很类似,不过windows我们是无法看到启动信息的,而linux启动时我们会看到许多启动信息,例如某个服务是否启动. Lin ...

  9. swift中UIImageView的创建

    let imageView = UIImageView() let imageView1 = UIImageView(frame: CGRectMake(, , , )) // 创建出来的UIImag ...

  10. UILabel的一些属性

    1.0  lineBreakMode 1.1.0 NSLineBreakByWordWrapping = 0,  以字符为显示单位显示,后面部分省略不显示 NSLineBreakByCharWrapp ...