网页学习体会

  • 首页
  • 个人博客
您的位置: 首页  >  IT文章  >  【LeetCode】191. Number of 1 Bits 题目: 提示: 代码:

【LeetCode】191. Number of 1 Bits 题目: 提示: 代码:

分类: IT文章 • 2022-10-18 00:21:15

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);
    }
};

相关推荐

  • LeetCode 191. Number of 1 Bits
  • 191. Number of 1 Bits Leetcode Python
  • 【LeetCode】136. Single Number 题目: 提示: 代码:
  • 【LeetCode】191. Number of 1 Bits 题目: 提示: 代码:
  • 【leetcode】Number of 1 Bits 题目描述: 解题思路:
  • leetcode 191. Number of 1 Bits
  • LeetCode OJ 之 Number of Digit One (数字1的个数) 题目: 思路: 代码:
  • LeetCode 191. Number of 1 Bits
  • LeetCode OJ 之 Number of Digit One (数字1的个数) 题目: 思路: 代码:
  • leetcode/2017-1-1 461. Hamming Distance 191. Number of 1 Bits 448. Find All Numbers Disappeared in an Array
  • IDEA在使用"svn"关联项目时报错Error:Cannot run program "svn" (in directory "路径XXXXXX"):CreateProcess error=2
  • js获取周几、获取前多少个小时
    网站免责声明 网站地图 最新文章 用户隐私 版权申明
本站所有数据收集于网络,如果侵犯到您的权益,请联系网站进行下架处理。   

Copyright © 2018-2021   Powered By 网页学习体会    备案号:   粤ICP备20002247号