Leetcode16: 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.

class Solution {
public:
    int hammingWeight(uint32_t n) {
        int k = 0;
        while(n)
        {
            k += (n & 0x1) > 0 ? 1 : 0; //(n % 2) > 0 ? 1 : 0;
            n >>= 1;                    //n /= 2;
        }
        return k;
    }
};

这是比较容易想到的解决办法,每次判断最后一位是否是1。但是这样32位的数最坏的情况要比较32次。有没有更简单的方法呢?

下面这种方法更为简单。假设n=
1111000111000 那 n-1 = 1111000110111,
(n-1) & n = 1111000110000,刚好把最后一个1给干掉了。也就是说,
(n-1)&n 刚好会从最后一位开始,每次会干掉一个1.这样速度就比上面哪种方法快了。有几个1,就执行几次。(学渣表示震惊!= =)

class Solution {
public:
    int hammingWeight(uint32_t n) {
        int k = 0;
        while(n != 0)
        {
            n = n & (n-1);
            k++;
        }
        return k;
    }
};

时间: 2024-08-11 03:36:31

Leetcode16: Number of 1 Bits的相关文章

191. 求1的位数 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 retu

LeetCode OJ:Number of 1 Bits(比特1的位数)

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 retu

[LeetCode]Number of 1 Bits

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 fun

Number of 1 Bits

Description: 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 functio

【leetcode】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 retu

LeetCode 191:number of one 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 shoul

191 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 retu

LeetCode 191. Number of 1 bits (位1的数量)

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 retu

LeetCode191 Number of 1 Bits. LeetCode231 Power of Two. LeetCode342 Power of Four

位运算相关 三道题 231. Power of Two Given an integer, write a function to determine if it is a power of two. (Easy) 分析: 数字相关题有的可以考虑用位运算,例如&可以作为筛选器. 比如n & (n - 1) 可以将n的最低位1删除,所以判断n是否为2的幂,即判断(n & (n - 1) == 0) 代码: 1 class Solution { 2 public: 3 bool isP