版权声明: 本文为博主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. Linux突然无法使用,是内存不足的问题

    昨晚发现这个问题是网站请求连不到Linux服务器了,后来发现是内存不足导致docker进程被杀掉了,docker的Status为Exit(137),是被内核杀掉的意思, 具体解释看这里,https:/ ...

  2. Extjs小总结

    1.绑定工作流:    INSERT INTO `base_codeclass` VALUES (null, 'PM_CGSQ', '采购申请', null, null, null, '2018-09 ...

  3. Android像素密度单位解析

    Android应用程序 res/drawable-hdpi drawable-xxhdpi 显示的不同 对比实验: 创建项目后,默认在相关文件目录中生成以下图标: hdpi --> 72px m ...

  4. 953.Verifying an Alien Dictionary(Map)

    In an alien language, surprisingly they also use english lowercase letters, but possibly in a differ ...

  5. Spark集群部署(standLone)模式

      安装部署: 1. 配置spark为1个master,2个slave的独立集群(Standlone)模式, 可以在VMWare中构建3台运行Ubuntu的机器作为服务器: master主机配置如下: ...

  6. 刚学的vue.js的单一事件管理组件通信

    第一次在博客园写的技术分享,写的不好的话各位大神多体谅,好啦进入主题 说说思路 首先 第一步,准备一个空的示例对象 var Event=new Vue(); 第二步,准备发送的数据 Event.$em ...

  7. Markdown常用快捷键

    Markdown使用的符号:井号,星号,大于号,中括号,竖线,横杠,波浪线,反引号 # ,*, > ,[],|,-,~,` 井号 + 空格:根据空格的个数显示各标题的大小 标题一 标题二 标题三 ...

  8. 201771010126 王燕《面向对象程序设计(Java)》第十七周学习总结

    实验十七  线程同步控制 实验时间 2018-12-10 1.实验目的与要求 (1) 掌握线程同步的概念及实现技术:  多线程并发运行不确定性问题解决方案: 多线程并发运行不确定性问题解决方案: 多 ...

  9. [LeetCode] Score After Flipping Matrix 翻转矩阵后的分数

    We have a two dimensional matrix A where each value is 0 or 1. A move consists of choosing any row o ...

  10. [LeetCode] Bus Routes 公交线路

    We have a list of bus routes. Each routes[i] is a bus route that the i-th bus repeats forever. For e ...