题目链接
https://leetcode.com/problems/number-of-1-bits/
题目原文
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.
题目翻译
写一个函数,输入一个无符号整数,输出这个整数二进制表示中有多少个 ’1‘ (该结果也被称为“Hamming weight”)。
比如,32位整数 ‘11’ 的二进制表示为 00000000000000000000000000001011 ,所以函数应该返回3。
思路方法
思路一
按照定义,将输入的数表示成二进制的字符串,数一下有多少个1即可。
代码
class Solution

该博客介绍了LeetCode中的第191题,即计算一个无符号整数二进制表示中1的个数(又称Hamming weight)。博主提供了三种解题思路,包括将数字转换为二进制字符串计数1、通过移位操作判断1以及利用位操作技巧计算1的个数,并附带了相应的Python代码实现。
最低0.47元/天 解锁文章
893

被折叠的 条评论
为什么被折叠?



