Class Solution
java.lang.Object
g0101_0200.s0191_number_of_1_bits.Solution
191 - Number of 1 Bits.<p>Easy</p>
<p>Write a function that takes an unsigned integer and returns the number of ‘1’ bits it has (also known as the <a href="http://en.wikipedia.org/wiki/Hamming_weight" target="_top">Hamming weight</a>).</p>
<p><strong>Note:</strong></p>
<ul>
<li>Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It should not affect your implementation, as the integer’s internal binary representation is the same, whether it is signed or unsigned.</li>
<li>In Java, the compiler represents the signed integers using <a href="https://en.wikipedia.org/wiki/Two%27s_complement" target="_top">2’s complement notation</a>. Therefore, in <strong>Example 3</strong> , the input represents the signed integer. <code>-3</code>.</li>
</ul>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> n = 00000000000000000000000000001011</p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong> The input binary string <strong>00000000000000000000000000001011</strong> has a total of three ‘1’ bits.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> n = 00000000000000000000000010000000</p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong> The input binary string <strong>00000000000000000000000010000000</strong> has a total of one ‘1’ bit.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> n = 11111111111111111111111111111101</p>
<p><strong>Output:</strong> 31</p>
<p><strong>Explanation:</strong> The input binary string <strong>11111111111111111111111111111101</strong> has a total of thirty one ‘1’ bits.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The input must be a <strong>binary string</strong> of length <code>32</code>.</li>
</ul>
<p><strong>Follow up:</strong> If this function is called many times, how would you optimize it?</p>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
hammingWeight
public int hammingWeight(int n)
-