[leetcode-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 return 3.

思路1:向右移位,判断最后一位即可。

思路2:

n & (n - 1) drops the lowest set bit. It‘s a neat little bit trick.

Let‘s use n = 00101100 as an example. This binary representation has three 1s.

If n = 00101100, then n - 1 = 00101011, so n & (n - 1) = 00101100 & 00101011 = 00101000. Count = 1.

If n = 00101000, then n - 1 = 00100111, so n & (n - 1) = 00101000 & 00100111 = 00100000. Count = 2.

If n = 00100000, then n - 1 = 00011111, so n & (n - 1) = 00100000 & 00011111 = 00000000. Count = 3.

n is now zero, so the while loop ends, and the final count (the numbers of set bits) is returned.

int hammingWeight(int n)
{
  int weight = 0;
    while(n != 0)
    {
       if(n & 1 == 1)weight++;
       n>>=1;
    }
    return weight;
 }
int hammingWeight(uint32_t n) {
    int count = 0;

    while (n) {
        n &= (n - 1);
        count++;
    }

    return count;
}

参考:

https://discuss.leetcode.com/topic/20120/c-solution-n-n-1

时间: 2024-11-04 00:43:11

[leetcode-191-Number of 1 Bits]的相关文章

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

Java for LeetCode 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 比特的数字们)

翻译 写一个函数获取一个无符号整型数,并且返回它的"1"比特的数目(也被叫做Hamming weight). 例如,一个32位整型数"11",转换成二进制是00000000000000000000000000001011,所以这个函数应该返回3. 原文 Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the

(easy)LeetCode 191.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

[LeetCode] 191. Number of 1 Bits ☆(位 1 的个数)

描述 Write a function that takes an unsigned integer and return the number of '1' bits it has (also known as the Hamming weight). 输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数. 解析 消除最后的1 观察一下 n 与 n-1 这两个数的二进制表示:对于 n-1 这个数的二进制来说,相对于 n 的二进制,它的最末位的一个 1 会变成 0,最末位一个 1

Leetcode 191 Number of 1 Bits 位运算

统计一个值的二进制1的个数,用与(&)和向左移位(<<) 编程之美中的2.1节有详细解答. 解法一: 1 class Solution { 2 public: 3 int hammingWeight(uint32_t n) { 4 int ans = 0; 5 for(;n;n = n>>1){ 6 ans += n&1; 7 } 8 return ans; 9 } 10 }; 解法二: 1 class Solution { 2 public: 3 int hamm

LeetCode 191. Number of 1 Bits QuestionEditorial Solution

题意:给你一个整数,计算该整数的二进制形式里有多少个“1”.比如6(110),就有2个“1”. 一开始我就把数字n不断右移,然后判定最右位是否为1,是就cnt++,否则就继续右移直到n为0. 可是题目说了是无符号整数,所以给了2147483648,就WA了. 因为java里的int默认当做有符号数来操作的,而2147483648超过int的最大整数,所以在int里面其实是当做-1来计算的. 那么,不能再while里面判断n是否大于0,和使用位操作符>>.应该使用位操作符>>>

LeetCode:Number of 1 Bits - 整数的汉明重量

1.题目名称 Number of 1 Bits(整数的汉明重量) 2.题目地址 https://leetcode.com/problems/number-of-1-bits/ 3.题目内容 英文:Write a function that takes an unsigned integer and returns the number of '1' bits it has. 中文:写一个函数,输入一个无符号整数,返回其中值为1的比特位的个数(这个值也被称为数字汉明重量) 例如,32位整型数字11

338. Counting Bits &amp;&amp; 191. Number of 1 Bits

Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example:For num = 5 you should return [0,1,1,2,1,2]. Follow up: It is very e

【LeetCode】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