版权声明: 本文为博主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. 为什么hexo预览功能总是间歇性失效?

    个人主题:yilia 有的文章可以正常截断,有的文章不行. 开始我以为这是hexo的一个小bug,但是后来通过查阅GitHub和知乎等网站发现这完全是由于我自己的粗心造成的…手动滑稽 hexo pag ...

  2. 爬虫之图片懒加载技术及js加密

    图片懒加载 图片懒加载概念: 图片懒加载是一种网页优化技术.图片作为一种网络资源,在被请求时也与普通静态资源一样,将占用网络资源,而一次性将整个页面的所有图片加载完,将大大增加页面的首屏加载时间.为了 ...

  3. centos6.5使用Google auth进行双因子认证

    1.环境 系统:centos6.5 x86_64 [root@uu ~]# uname -a Linux uu 2.6.32-642.el6.x86_64 #1 SMP Wed Apr 13 00:5 ...

  4. DW1000 用户手册中文版 附录3:双向测距(Two-Way Ranging)

    由于已经在wode中排版无法直接复制到博客中,故本节博客发布使用了图片. 论坛可下载PDF  http://bphero.com.cn/forum.php?mod=viewthread&tid ...

  5. tensorflow 使用 3 模型学习

    模型学习 import tensorflow as tf import numpy as np # 生成 100 个随机的点 x_data = np.random.rand( 100 ) y_data ...

  6. C++第一课:基本语法for Visual Studio 2015[个人见解]

    在学习C++时,或许不了解情况的人会问:到底先学习C语言还是C++,哪个更好? 那么小编的个人见解是:你在学习时别管哪个语言好与不好,是个语言它都是好语言,关键在于你会挖掘其中存在的价值,C++可以说 ...

  7. 初次部署django+gunicorn+nginx

    初次部署django+gunicorn+nginx  博客详细地址  https://www.cnblogs.com/nanrou/p/7026802.html 写在前面,这只是我所遇到的情况,如果有 ...

  8. h5、css3基础

    一.html(超文本标记语言) 作用:实现页面布局 页面由许多标记符号组成 由浏览器解释执行 二.html主题创建方式 !(英文状态)+tab html:4s+tab html:5+tab 三.标签 ...

  9. libevent入门介绍

    libevent是之前看到的一个别人推荐的清凉级网络库,我就想了解一下它.今天下载到了一个人写的剖析系列,从结构和源码方面进行了简要分析.只是这个分析文章是2010年的,有点过时了(跟现在的libev ...

  10. Luogu P1381油滴扩展

    传送门 数据范围给的很小啊,n >= 0 && n <= 7,所以给了DFS生存的空间. 对于每一个油滴,可以说在它下一个油滴放置之前,当前的这个油滴的半径并不确定(但是对 ...