题目描述:

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

Example 1:

Input: 11
Output: 3
Explanation: Integer 11 has binary representation 00000000000000000000000000001011 

Example 2:

Input: 128
Output: 1
Explanation: Integer 128 has binary representation 00000000000000000000000010000000

要完成的函数:

int hammingWeight(uint32_t n)

说明:

1、这道题目给定一个数n,要求转换为二进制之后,数一下有多少个1,输出1的个数。

2、明白题意,这道题用位操作完成,十分容易。

代码如下:(附详解)

    int hammingWeight(uint32_t n) {int res=0;for(int i=0;i<32;i++){res+=(n&1);//取出最后一位,如果是1那么加上去,如果是0加上去也一点影响都没有,避开了条件判断n>>=1;//n右移一位}return res;}

上述代码实测4ms,beats 100% of cpp submissions。

转载于:https://www.cnblogs.com/chenjx85/p/9107743.html

leetcode-191-Number of 1 Bits相关推荐

  1. LeetCode 191 Number of 1 Bits

    LeetCode 191 Number of 1 Bits 解法一(较为传统都解法):使用将n不断右移,并与1想&得到1的个数:(也有使用除法/2的,明显除法的运行效率要低于位移) 时间复杂度 ...

  2. [勇者闯LeetCode] 191. Number of 1 Bits

    [勇者闯LeetCode] 191. Number of 1 Bits Description Write a function that takes an unsigned integer and ...

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

  4. LeetCode - Easy - 191. Number of 1 Bits

    Topic Bit Manipulation Description https://leetcode.com/problems/number-of-1-bits/ Write a function ...

  5. leetcode python3 简单题191. Number of 1 Bits

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百九十一题 (1)题目 英文: Write a function that ta ...

  6. 【LeetCode从零单排】No 191.Number of 1 Bits(考察位运算)

    题目 Write a function that takes an unsigned integer and returns the number of '1' bits it has (also k ...

  7. 【LeetCode】191. Number of 1 Bits

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

  8. Leet Code OJ 191. Number of 1 Bits [Difficulty: Easy]

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

  9. 【Leetcode】Number of 1 Bits(easy)

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

  10. 191. Number of 1 Bits

最新文章

  1. 2022-2028年中国餐具行业市场研究及前瞻分析报告
  2. VSCode中.py文件找不到路径的解决办法
  3. android ImageButton的图片怎么定义?
  4. Python语言学习之双下划线那些事:python和双下划线使用方法之详细攻略
  5. 沉得住气的程序员们!
  6. Qt之格栅布局(QGridLayout)
  7. Blackcat主题-Wordpress
  8. Json.Net Demo2
  9. Golang实践录:静态资源文件整合:初步使用
  10. bzoj1770: [Usaco2009 Nov]lights 燈(折半搜索)
  11. linux 的空命令:(冒号)
  12. javascript设计模式 ---序
  13. 蓝桥杯2015年第六届C/C++B组第六题-加法变乘法
  14. 高级研发工程师都有哪些特点?快来看看你是否符合【超级准】
  15. 微信小程序base64转为二维码、条形码图片
  16. 一次因为游戏程序员猝死引发的思考
  17. formidable词根词缀_实用文档之英语单词(词根词缀)
  18. gee学习1geemap入门(环境搭建和地图创建)
  19. 阿里云视觉AI训练营_Class3_文字识别项目讲解及使用说明
  20. 怎么使用dbc2000架设传奇-dbc2000架设传奇教程

热门文章

  1. webstorm 不知道手贱点了什么,有时候会自动删除分号
  2. 命令行分析java线程CPU占用
  3. 固定资产打开提示:上年度数据未结转!
  4. 人脸颜值评分软件_在线算个颜值,特科学的那种 | 知多少
  5. 树莓派作为客户端与WemosD1作为服务器的无线通信(局域网通信)
  6. 序列化与反序列化(1)Serializable —— Java原生态方法
  7. python中numpy模块的around方法_Python numpy.around()用法及代码示例
  8. python is beautiful_Python list 和 str 互转
  9. python汇率编程_【菜鸟学Python】案例一:汇率换算
  10. 图解java工程师学习路线