LeetCode:Number of 1 Bits

Problems:

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.

Solution:

 1 class Solution {
 2 public:
 3     int hammingWeight(unsigned int  n) {
 4
 5     int i=0;
 6     while(n>0)
 7     {
 9        int temp = n&0x1;
10        if(temp==1)
11        {
12         i++;
13        }
14        n=n>>1;
15     }
16     return i;
17
18     }
19 };
时间: 2024-10-10 16:10:36

LeetCode:Number of 1 Bits的相关文章

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

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

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 (11) 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

[LeetCode][JavaScript]Number of 1 Bits

https://leetcode.com/problems/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 representat

(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] Prime Number of Set Bits in Binary Representation

Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime number of set bits in their binary representation. (Recall that the number of set bits an integer has is the number of 1s present when written in bin