题目:

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

For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3.

提示:

考察的是程序递归调用,以及对二进制表示的理解。

代码:

class Solution {
public:int hammingWeight(uint32_t n) {if (!n) return 0;return (n % 2) + hammingWeight(n >> 1);}
};

转载于:https://www.cnblogs.com/jdneo/p/4738942.html

【LeetCode】191. Number of 1 Bits相关推荐

  1. 【Leetcode】Palindrome Number

    Determine whether an integer is a palindrome. Do this without extra space. 思路:若使用[Leetcode]Reverse I ...

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

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

  3. 【LeetCode】Palindrome Number(回文数)

    这道题是LeetCode里的第9道题. 题目说的: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: ...

  4. 【leetcode】Single Number (Medium) ☆

    题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...

  5. 【leetcode】726. Number of Atoms

    题目如下: 解题思路:我用的是递归的方法,每次找出与第一个')'匹配的'('计算atom的数量后去除括号,只到分子式中没有括号为止.例如 "K4(ON(SO3)2)2" -> ...

  6. LeetCode 191 Number of 1 Bits

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

  7. leetcode【537】Complex Number Multiplication(复数相乘)

    写在最前面:一道很常规的字符串分割的题 leetcode[537]Complex Number Multiplication Given two strings representing two co ...

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

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

  9. 【leetcode】解题日记(未完待续)

    开坑,有生之年系列,希望有一天能解出 leetcodeleetcodeleetcode 上的所有题目. 写题解好麻烦,懒得写(手动狗头),进度如下,不定期更新. 总题数 已解答 题解数 2058 23 ...

最新文章

  1. 人工智能技术进阶路线
  2. IE8 CSS HACK 兼容集合
  3. 多台工作站搭建MPI并行环境
  4. oracle 如何查看日志?
  5. target not created怎么解决_怎么才能最短时、高效、踏实地学习 Python(附链接)...
  6. 渝粤教育 陕西师范大学 《证券投资学[专升本]》作业
  7. matlab的数学函数,matlab中常见数学函数的使用
  8. crontab下执行设置壁纸出错问题
  9. Qt 多界面来回切换的问题以及Qt界面关闭与销毁
  10. python 摄像头采集_Python+OpenCV采集本地摄像头的视频
  11. 自动量化炒股软件是什么?
  12. java 人民币大写_Java实现人民币大写精讲
  13. Stepping Number
  14. Python 蓝凌OA任意文件读取批量扫描 poc编写
  15. 【Python】pass,continue和break的区别
  16. ios 微信分享重新编码链接_iOS 微信分享文件【原创】
  17. 怎么抠图最简单?教你一行Python代码去除照片背景
  18. 第四天--外边距塌陷
  19. vue移动端手机号正则表达式
  20. 中标麒麟NeokylinV7关闭广播消息

热门文章

  1. 认识代码编辑区域与解决方案区域 005
  2. 游戏设计模式思考:“穿越火线”中的“策略模式”
  3. Cocos 技术派:实时竞技小游戏技术实现分享
  4. 鹅厂又赢了!游戏不再是玩家的资产,我们只有“使用权”不能交易
  5. oracle 11gR2 新增deinstall命令来卸载grid和database
  6. phoenix kerberos 连接配置
  7. 10如何成为卓越领导者摘录——卓越的领导者
  8. angular_ui-router ——依赖注入
  9. 【好程序员笔记分享】——UIView与CALayer详解
  10. scrum 12.2