问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4052 访问。

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

输入: 11

输出: 3

解释: 整数 11 的二进制表示为 00000000000000000000000000001011

输入: 128

输出: 1

解释: 整数 128 的二进制表示为 00000000000000000000000010000000


Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight).

Input: 11

Output: 3

Explanation: Integer 11 has binary representation 00000000000000000000000000001011

Input: 128

Output: 1

Explanation: Integer 128 has binary representation 00000000000000000000000010000000


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4052 访问。

  1. public class Program {
  2. public static void Main(string[] args) {
  3. var n = 11U;
  4. var res = HammingWeight(n);
  5. Console.WriteLine(res);
  6. n = 128U;
  7. res = HammingWeight2(n);
  8. Console.WriteLine(res);
  9. Console.ReadKey();
  10. }
  11. public static int HammingWeight(uint n) {
  12. //10进制转2进制,除2取余法
  13. var count = 0;
  14. while(n != 0) {
  15. if(n % 2 != 0) count++;
  16. n /= 2;
  17. }
  18. return count;
  19. }
  20. public static int HammingWeight2(uint n) {
  21. //基本思路同 HammingWeight
  22. var count = 0;
  23. while(n != 0) {
  24. //10进制的1,2,3,4,5对应于2进制的1,10,11,100,101
  25. //由于2进制的特点,末位数在1,0之间循环
  26. //用 n 和 1 做“与运算”若值为1,必为奇数
  27. //即除2余1
  28. if((n & 1) == 1) count++;
  29. //2进制右移1位即10进制除2
  30. n >>= 1;
  31. }
  32. return count;
  33. }
  34. }

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4052 访问。

  1. 3
  2. 1

分析:

显而易见,以上2种算法的时间复杂度均为:  。

C#LeetCode刷题之#191-位1的个数(Number of 1 Bits)的更多相关文章

  1. leetcode刷题笔记191 位1的个数

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

  2. [Swift]LeetCode191. 位1的个数 | Number of 1 Bits

    Write a function that takes an unsigned integer and return the number of '1' bits it has (also known ...

  3. C#LeetCode刷题之#258-各位相加(Add Digits)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3860 访问. 给定一个非负整数 num,反复将各个位上的数字相加 ...

  4. C#LeetCode刷题之#374-猜数字大小(Guess Number Higher or Lower)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3993 访问. 我们正在玩一个猜数字游戏. 游戏规则如下: 我从 ...

  5. C#LeetCode刷题之#9-回文数(Palindrome Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3840 访问. 判断一个整数是否是回文数.回文数是指正序(从左向右 ...

  6. C#LeetCode刷题之#202-快乐数(Happy Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3856 访问. 编写一个算法来判断一个数是不是"快乐数& ...

  7. C#LeetCode刷题之#507-完美数(Perfect Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3879 访问. 对于一个 正整数,如果它和除了它自身以外的所有正因 ...

  8. C#LeetCode刷题之#268-缺失数字(Missing Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4056 访问. 给定一个包含 0, 1, 2, ..., n 中  ...

  9. C#LeetCode刷题-位运算

    位运算篇 # 题名 刷题 通过率 难度 78 子集   67.2% 中等 136 只出现一次的数字 C#LeetCode刷题之#136-只出现一次的数字(Single Number) 53.5% 简单 ...

  10. C#LeetCode刷题-数学

    数学篇 # 题名 刷题 通过率 难度 2 两数相加   29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 ...

随机推荐

  1. Ubuntu18.04安装GitLab搭建私有仓库服务器过程笔记

      百度了很多资料结果折腾很久还没安装成功,索性就直接上官网找文档参考顺利搭建完成 因为有2台服务器做练习,总结了2种安装方式提供参考:第一种官网安装方式,第二种国内镜像安装方式(建议采用第二种) 第 ...

  2. Python 实现邮件发送功能(进阶)

    上篇文章已经介绍了利用Python发送文本消息的用法,也在文末遗留了如何发送图片和附件的问题,本章主要来回答这两个问题.   本章主要包含知识点: 1. 如何将图片放到邮件主体中发送 2. 如何发送附 ...

  3. 写给.NET开发者的Python教程(二):基本类型和变量

    从本文开始,我们就要正式了解Python的语法特性了,这章主要介绍基本类型和变量,开始之前先介绍下Python中的标准输入输出. 标准输入输出 前文举过TwoSum问题的例子,但是没有讲到标准输入输出 ...

  4. 如果你还不知道如何控制springboot中bean的加载顺序,那你一定要看此篇

    1.为什么需要控制加载顺序 springboot遵从约定大于配置的原则,极大程度的解决了配置繁琐的问题.在此基础上,又提供了spi机制,用spring.factories可以完成一个小组件的自动装配功 ...

  5. 一个文本框的各项说明,包括类似html的table

    https://www.cnblogs.com/zhqiux/archive/2013/09/03/3298654.html

  6. python的__get__方法看这一篇就足够了

    get类型函数 直接上代码: class TestMain: def __init__(self): print('TestMain:__init__') self.a = 1 if __name__ ...

  7. 论文阅读 ORBSLAM3

    这周末ORB-SLAM3出现了.先看了看论文.IMU部分没细看,后面补上. Abstract 视觉,视觉惯导,多地图SLAM系统 支持单目/立体/RGBD相机 支持pinhole/鱼眼相机 基于特征/ ...

  8. 利用div显示隐藏实现的分页效果

    实现步骤: 1.创建对应切换div <div class="bottom_daohang"> <div class="bottom_daohang_zo ...

  9. Pattern、Matcher的用法

    Pattern和Matcher Pattern 一个Pattern是一个正则表达式经编译后的表现模式. Matcher 一个Matcher对象是一个状态机器,它依据Pattern对象做为匹配模式对字符 ...

  10. Fortify Audit Workbench 笔记 Dynamic Code Evaluation: Code Injection

    Dynamic Code Evaluation: Code Injection Abstract 在运行时中解析用户控制的指令,会让攻击者有机会执行恶意代码. Explanation 许多现代编程语言 ...