编写一个函数,输入是一个无符号整数,返回的是它所有 位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的个数的更多相关文章

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

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

  3. [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 ...

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

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

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

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

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

  8. LeetCode 191 Number of 1 Bits

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

  9. 191. Number of 1 Bits

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

随机推荐

  1. Web开发从零单排之一:在新浪云平台SAE上开发一个html5电子喜帖

    需求描述: 本人大婚将至,女朋友说“现在都流行在微信上发电子请帖了,你不是技(cheng)术(xu)宅(yuan)嘛,不会连这个都搞不定吧” 本人嘴上说这等小事何足挂齿,但心里还是七上八下的,虽然自认 ...

  2. 使用RNN解决句子对匹配问题的常见网络结构

    /* 版权声明:能够随意转载,转载时请标明文章原始出处和作者信息 .*/ author: 张俊林 除了序列标注问题外,句子对匹配(Sentence Pair Matching)问题也是NLP中非经常见 ...

  3. Intel为Google的物联网平台Brillo推出开发板Edison

    Brillo* is a solution from Google* for building connected devices. Incorporating aspects of the Andr ...

  4. ARGB,RGB颜色值表示

    转载请注明出处:http://blog.csdn.net/wei_chong_chong/article/details/50831493 今天自己定义一个控件.设置背景颜色时犯难了 如今就来总结一下 ...

  5. Ios 项目从头开发 MVVM模式(三)

    1.话说,本来想做个聚合查询功能.可是我的重点想研究xmpp聊天功能.所以使用mvvm模式做了全然模式51job主界面的页面. 2.首先给大家看我执行起来的界面. 3.界面非常easy,做这个界面主要 ...

  6. Linux上ln命令详细说明及软链接和硬链接的区别

    硬链接(hard link) UNIX文件系统提供了一种将不同文件链接至同一个文件的机制,我们称这种机制为链接.它可以使得单个程序对同一文件使用不同的名字.这样的好处是文件系 统只存在一个文件的副本, ...

  7. idea导出配置

  8. Can't remove netstandard folder from output path (.net standard)

    https://developercommunity.visualstudio.com/content/problem/30940/cant-remove-netstandard-folder-fro ...

  9. YTU 2893: F--Mark的双薪

    2893: F--Mark的双薪 时间限制: 1 Sec  内存限制: 128 MB 提交: 230  解决: 17 题目描述 程序员 Mark 编制的 PPYU 游戏终于上线.Mark 认为自己起早 ...

  10. js用法2

    1,网站cookie document.cookie 2, Web Storage相当于cookie,当存储量大于cookie localStorage 存储格式都是字符串 有效期,清空缓存前,永远存 ...