mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 11:43:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			46 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			46 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>Given two integers <code>left</code> and <code>right</code>, return <em>the <strong>count</strong> of numbers in the <strong>inclusive</strong> range </em><code>[left, right]</code><em> having a <strong>prime number of set bits</strong> in their binary representation</em>.</p>
 | 
						|
 | 
						|
<p>Recall that the <strong>number of set bits</strong> an integer has is the number of <code>1</code>'s present when written in binary.</p>
 | 
						|
 | 
						|
<ul>
 | 
						|
	<li>For example, <code>21</code> written in binary is <code>10101</code>, which has <code>3</code> set bits.</li>
 | 
						|
</ul>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<p><strong class="example">Example 1:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>Input:</strong> left = 6, right = 10
 | 
						|
<strong>Output:</strong> 4
 | 
						|
<strong>Explanation:</strong>
 | 
						|
6  -> 110 (2 set bits, 2 is prime)
 | 
						|
7  -> 111 (3 set bits, 3 is prime)
 | 
						|
8  -> 1000 (1 set bit, 1 is not prime)
 | 
						|
9  -> 1001 (2 set bits, 2 is prime)
 | 
						|
10 -> 1010 (2 set bits, 2 is prime)
 | 
						|
4 numbers have a prime number of set bits.
 | 
						|
</pre>
 | 
						|
 | 
						|
<p><strong class="example">Example 2:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>Input:</strong> left = 10, right = 15
 | 
						|
<strong>Output:</strong> 5
 | 
						|
<strong>Explanation:</strong>
 | 
						|
10 -> 1010 (2 set bits, 2 is prime)
 | 
						|
11 -> 1011 (3 set bits, 3 is prime)
 | 
						|
12 -> 1100 (2 set bits, 2 is prime)
 | 
						|
13 -> 1101 (3 set bits, 3 is prime)
 | 
						|
14 -> 1110 (3 set bits, 3 is prime)
 | 
						|
15 -> 1111 (4 set bits, 4 is not prime)
 | 
						|
5 numbers have a prime number of set bits.
 | 
						|
</pre>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<p><strong>Constraints:</strong></p>
 | 
						|
 | 
						|
<ul>
 | 
						|
	<li><code>1 <= left <= right <= 10<sup>6</sup></code></li>
 | 
						|
	<li><code>0 <= right - left <= 10<sup>4</sup></code></li>
 | 
						|
</ul>
 |