leetcode 191

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.

求出输入整数的32位中有多少位值为1.

代码如下:

 1 class Solution {
 2 public:
 3     int hammingWeight(uint32_t n) {
 4         unsigned pos = 1 << 31;
 5         int count = 0;
 6         while(pos)
 7         {
 8             if(pos & n)
 9             {
10                 count++;
11             }
12             pos >>= 1;
13         }
14         return count;
15     }
16 };
时间: 2024-10-08 19:35:19

leetcode 191的相关文章

统计二进制中1的个数(LeetCode 461. 汉明距离 or LeetCode 191. 位1的个数)

题目一 LeetCode 461.明距离(Hamming Distance) 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目.给出两个整数 x 和 y,计算它们之间的汉明距离. 注意 0 ≤ x , y < 231. 原文地址:https://www.cnblogs.com/hglibin/p/8984777.html

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

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

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

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#191]Number of Bits

Problem: 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 sh

(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