版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址

http://blog.csdn.net/lzuacm

C#版 - Leetcode 191. Number of 1 Bits题解

191. 位1的个数

在线提交:

https://leetcode-cn.com/problems/number-of-1-bits/description/

题目描述


编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量)。

示例 :

输入: 11
输出: 3
解释: 整数 11 的二进制表示为 00000000000000000000000000001011

示例 2:

输入: 128
输出: 1
解释: 整数 128 的二进制表示为 00000000000000000000000010000000

  ●  题目难度: Easy
  • 通过次数:2K
  • 提交次数:4.9K

  • 相关话题 位运算

思路:

使用n = n&(n-1)进行迭代,每进行一次,将最右侧存有1的bit的值置为0,直到全0,终止计数。

已AC代码:

public class Solution
{
    public int HammingWeight(uint n)
    {
        int count = 0;
        while (n > 0)
        {
            n = n & (n - 1);
            count++;
        }                

        return count;
    }
}

C#版 - Leetcode 191. Number of 1 Bits-题解的更多相关文章

  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 (位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

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

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

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

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

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

随机推荐

  1. 极速创建 IOS APP !涛舅舅苹果 IOS APP自助生成系统!不用证书、不用越狱、永久可用

    不用签名将网页封装成苹果APP,无需苹果企业签名,IPA签名,ios签名,免越狱安装 (本方法只支持网站封装app,原生的用不了,详细请咨询客服) 近期很多朋友问我把网站变成app的方法,原因很多种, ...

  2. hive动态分区和混合分区

    各位看官,今天我们来讨论下再Hive中的动态分区和混合分区方面的一些知识点以及相关的一些问题. 前面我们已经讲过管理表和外部表的一般分区的一些知识点,对于需要对表创建很多的分区,那么用户就需要些很多的 ...

  3. Java拦截器的实现原理

    对于某个类的A方法进行拦截,在A执行前插入一段代码,A执行后也插入一段代码 原理: 写个拦截器,拦截器中包含要插入前后执行的两段代码 interceptor { C();//C方法 D();//D方法 ...

  4. 如何在浏览器中输入(myeclipse创建的项目的)地址访问JSP页面

    如何在浏览器中输入(myeclipse创建的项目的)地址访问JSP页面 可以在Tomcat项目里面查看你的JSP页面在哪里,具体的路径为: tomcat--work--localhost--项目名称, ...

  5. 一个'&'引起md5签名不一致问题

    有时会遇到这样一个问题,本地和接口在验证数据签名时,明明两端打印出来的两个字符串一模一样,但是md5加密后的两个密文却不一样.例如:本地字符串:$str = "a=1&b=2& ...

  6. Python基础之自定义工具类

    class ListHelper: @staticmethod def find_all(target, func_condition): """ 查找列表中满足条件的所 ...

  7. C# 使用NPOI 处理Excel(Datable与Excel相互转换)

    VS上有自带的程序集可以读取,但是总是会出现这样或那样的问题,让人恨得牙疼!而且效率太慢了.用NPOI就好多了,比较快,而且稳定,还简单,引用相应的程序集就好了. Excel转换成Datable pr ...

  8. 关于使用freemarker导出文档的使用

    7.FreeMarker导出word文件,模板:template.ftl/** * 为word加载数据插值 * * @throws IOException */ public void exportW ...

  9. C语言面试题分类->指针

    有关指针的经典面试题 C语言为何如此长寿并实用?C++为什么有那么多精彩?指针可以说是C/C++中的灵魂所在,虽然早期中pascal也有指针,但是和C/C++比起来不是一个级别的.今天为大家深入浅出的 ...

  10. ubuntu 修改系统时间无效

    用root用户修改服务器时间无效:使用hwclock -w也不行 解决方法: 需要取消自动从互联网同步时间才可以的 timedatectl set-ntp 0 上面的命令可以关闭自动同步,然后你再设置 ...