LeetCode - 191. Number of 1 Bits

博客介绍了如何使用位运算来统计无符号整数的二进制表示中1的个数。通过JavaScript、Python和C#三种编程语言展示了实现该功能的代码示例,利用位移和按位与操作来逐位统计1的个数。

LeetCode - 191. Number of 1 Bits

在算法的世界里,我们常常会遇到各种有趣且富有挑战性的问题。今天,让我们聚焦于 LeetCode 上的第 191 题 ——“Number of 1 Bits”,深入探讨如何高效地解决这类问题。

1. 问题描述

题目要求我们编写一个函数,输入是一个无符号整数(以二进制串的形式),函数需要返回其二进制表达式中数字位数为 ‘1’ 的个数。这就好比我们拿到一个由 0 和 1 组成的二进制串,要做的是仔细数一数其中 1 的个数。例如,输入的无符号整数转换为二进制后是 1011,那么函数应该返回 3,因为这个二进制串中有 3 个 1。

2. 解题思路(位运算)

2.1 关键点:取二进制位

位运算在解决这类问题时发挥着关键作用。我们知道,计算机在存储数据时,是以二进制的形式进行的。而位运算允许我们直接对这些二进制位进行操作,这使得我们能够高效地处理与二进制相关的问题。

2.2 解题步骤:

  • a. 遍历数字每一位:我们需要一种方法来依次检查二进制数的每一位。这里,我们可以利用循环来实现对每一位的遍历。
  • b. 统计 1 的个数:在遍历每一位的过程中,我们要判断当前位是否为 1。如果是 1,就将计数器加 1。

下面是使用 JavaScript 和 Python 实现的代码示例:

2.3 JavaScript
/**
 * 位运算取位
 * @param {number} n - a positive integer
 * @return {number}
 */
var hammingWeight = function (n) {
    let count = 0;
    while (n) {
        count += n & 1;
        n = n >>> 1;
    }
    return count;
};

在这段 JavaScript 代码中,我们定义了一个变量count用于统计 1 的个数。通过while循环,只要n不为 0,就继续循环。在每次循环中,使用n & 1来判断n的最低位是否为 1,如果是则将count加 1。然后通过n = n >>> 1n无符号右移一位,继续检查下一位。

2.4 Python
class Solution:
    def hammingWeight(self, n: int) -> int:
        count = 0
        while n:
            # 注意运算优先级
            count = count + (n & 1) 
            n = n >> 1
        return count

Python 代码的思路与 JavaScript 类似。在Solution类中定义了hammingWeight方法,同样使用count变量统计 1 的个数。while循环中,通过n & 1判断当前位是否为 1 并更新count,然后通过n = n >> 1n右移一位。需要注意的是,在 Python 中,整数默认是有符号的,但这里题目输入是无符号整数,在实际应用场景中,如果输入确实是无符号整数,要确保输入范围在合理的无符号整数范围内,避免潜在的符号问题。

3. 解题思路(转二进制)

除了使用位运算直接操作二进制位,我们还可以通过将整数转换为二进制字符串,然后统计字符串中 1 的个数来解决这个问题。以下是 C# 语言的实现代码:

3.1 C#
public class Solution
{
    public int HammingWeight(uint n)
    {
        var count = 0;
        while (n != 0)
        {
            if (n % 2 == 1)
                count++;
            n /= 2;
        }

        return count;
    }
}

在这段 C# 代码中,在Solution类中定义了HammingWeight方法,接收一个无符号整数n。通过while循环,只要n不为 0 就继续循环。在循环内部,通过n % 2 == 1判断n的当前最低位是否为 1,如果是则将count加 1,然后通过n /= 2n除以 2,相当于将二进制数右移一位,继续检查下一位。这种方法虽然思路直观,但相较于位运算,在效率上可能会稍逊一筹,因为转换为二进制字符串以及逐字符统计的操作相对复杂一些。

通过对 LeetCode 191 题的深入分析和不同解法的探讨,我们不仅掌握了如何解决计算二进制中 1 的个数这类问题,还了解了位运算在实际编程中的强大应用以及不同编程语言在处理这类问题时的特点和实现方式。希望这些知识和思路能为大家在算法学习和编程实践中提供有益的帮助。

### LeetCode Problems Involving Counting the Number of 1s in Binary Representation #### Problem Description from LeetCode 191. Number of 1 Bits A task involves writing a function that receives an unsigned integer and returns the quantity of '1' bits within its binary form. The focus lies on identifying and tallying these specific bit values present in any given input number[^1]. ```python class Solution: def hammingWeight(self, n: int) -> int: count = 0 while n: count += n & 1 n >>= 1 return count ``` This Python code snippet demonstrates how to implement the solution using bitwise operations. #### Problem Description from LeetCode 338. Counting Bits Another related challenge requires generating an output list where each element represents the amount of set bits ('1') found in the binary notation for integers ranging from `0` up to a specified value `n`. This problem emphasizes creating an efficient algorithm capable of handling ranges efficiently[^4]. ```python def countBits(num): result = [0] * (num + 1) for i in range(1, num + 1): result[i] = result[i >> 1] + (i & 1) return result ``` Here, dynamic programming principles are applied alongside bitwise shifts (`>>`) and AND (`&`) operators to optimize performance during computation. #### Explanation Using Brian Kernighan Algorithm For optimizing further especially with large inputs, applying algorithms like **Brian Kernighan** offers significant advantages due to reduced iterations needed per operation compared against straightforward methods iterating through all possible positions or dividing repeatedly until reaching zero. The core idea behind this method relies upon subtracting powers-of-two corresponding only to those places holding actual ‘ones’ thereby skipping over zeroes entirely thus reducing unnecessary checks: ```python def hammingWeight(n): count = 0 while n != 0: n &= (n - 1) count += 1 return count ``` --related questions-- 1. How does the Hamming weight calculation differ between signed versus unsigned integers? 2. Can you explain why shifting right works effectively when determining counts of one-bits? 3. What optimizations exist beyond basic iteration techniques for calculating bit counts? 4. Is there any difference in implementation logic required across various programming languages supporting similar syntaxes? 5. Why might someone choose the Brian Kernighan approach over other strategies?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿蒙Armon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值