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

Solution

Idea: convert a unsigned integer into binary number

class Solution {
public:
int hammingWeight(uint32_t n) {
int ret = 0;
while (n!=0){
ret += n%2;
n = n/2;
}
return ret;
}
};

191. Number of 1 Bits [easy] (Python)

2016-05-05 15:13:57

leetcode 191. number of 1 bits

2016-08-15 23:20:09

191. Number of 1 Bits LeetCode

2016-02-10 13:10:19

[LeetCode] 191. Number of 1 Bits

2016-09-22 13:09:38

leetcode - 191. Number of 1 Bits

2018-02-01 20:26:36

191.[Leetcode]Number of 1 Bits

2016-05-02 19:30:39

leetcode 191. Number of 1 Bits

2016-04-01 11:25:07

(LeetCode) 191. Number of 1 Bits

2017-03-21 00:42:43

[Leetcode]191. Number of 1 Bits

2016-07-08 15:26:56

LeetCode 191. Number of 1 Bits

2015-05-04 13:41:28