编写一个函数,输入是一个无符号整数,返回的是它所有 位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. spring 学习资料

    spring 官方教程 1.3.1 http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/html/index.html ...

  2. 架构设计经典案例:X窗体系统

    X Window在1984年由MIT研发.它的设计哲学之中的一个是:提供机制.而非策略(类似面向对象思想中的"针对接口编程,而不是针对实现编程").机制(mechanism)是指须 ...

  3. grep命令使用技巧

    grep如何实现全词查找例如:要查找name这个单词,反馈的查找结果不能包含namespace这样的模式,但是可以包含name()这样的模式,即要查找的单词两端不可以有其他的数字或者字母,但可以有空格 ...

  4. java里int类型转byte类型

    今天在做书上的一个例子的时候, 要使用byte类型,首先我很直接的就写到了byte b = 0XAA, 结果报错, 说从int转换到byte可能会有损失. 我当时就很奇怪, 为什么会出现这种情况呢? ...

  5. Redhat Linux 下安装Oracle 11g R2

    能够下载:http://download.csdn.net/detail/ykh554541184/8086647文档方便查阅 官方文档:http://docs.oracle.com/cd/E1188 ...

  6. Form content types

    Forms in HTML documents https://www.w3.org/TR/html401/interact/forms.html#h-17.13.4 17.13.4 Form con ...

  7. 检查 统计 异常 通信 time_wait

    [root@hadoop1 conf]# netstat -n | grep -v 127.0.0.1 | grep -v :3306  | grep TIME_WAIT | sort -k 5n | ...

  8. Android vector 标签 pathData 详解

    转载地址:http://www.jianshu.com/p/a3cb1e23c2c4#rd Android Support Library 23.2 出来以后,在Android 5.0(API级别21 ...

  9. JAVASCRIPT 和 AJax 实现局部验证

    JSP页面 <td width="10%" class="main_matter_td">真实姓名</td> <td width= ...

  10. 使用C#开发HTTP服务器系列之访问主页

    各位朋友大家好,我是秦元培,欢迎大家关注我的博客,我的博客地址是http://qinyuanpei.com.在这个系列文章的第一篇中,我们着重认识和了解了HTTP协议,并在此基础上实现了一个可交互的W ...