Skip to content

Commit 7a911fd

Browse files
committed
feat: add solutions to lc problem: No.0762
No.0762.Prime Number of Set Bits in Binary Representation
1 parent 0e640ea commit 7a911fd

File tree

9 files changed

+172
-36
lines changed

9 files changed

+172
-36
lines changed

solution/0700-0799/0762.Prime Number of Set Bits in Binary Representation/README.md

Lines changed: 79 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,63 +6,126 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>给定两个整数&nbsp;<code>L</code>&nbsp;&nbsp;<code>R</code>&nbsp;,找到闭区间&nbsp;<code>[L, R]</code>&nbsp;范围内,计算置位位数为质数的整数个数。</p>
9+
<p>给你两个整数&nbsp;<code>left</code>&nbsp;&nbsp;<code>right</code> ,在闭区间 <code>[left, right]</code>&nbsp;范围内,统计并返回 <strong>计算置位位数为质数</strong> 的整数个数。</p>
1010

11-
<p>(注意,计算置位代表二进制表示中1的个数。例如&nbsp;<code>21</code>&nbsp;的二进制表示&nbsp;<code>10101</code>&nbsp;有 3 个计算置位。还有,1 不是质数。)</p>
11+
<p><strong>计算置位位数</strong> 就是二进制表示中 <code>1</code> 的个数。</p>
1212

13-
<p><strong>示例 1:</strong></p>
13+
<ul>
14+
<li>例如, <code>21</code>&nbsp;的二进制表示&nbsp;<code>10101</code>&nbsp;有 <code>3</code> 个计算置位。</li>
15+
</ul>
16+
17+
<p>&nbsp;</p>
18+
19+
<p><strong>示例 1:</strong></p>
1420

1521
<pre>
16-
<strong>输入:</strong> L = 6, R = 10
17-
<strong>输出:</strong> 4
18-
<strong>解释:</strong>
22+
<strong>输入</strong>left = 6, right = 10
23+
<strong>输出</strong>4
24+
<strong>解释</strong>
1925
6 -&gt; 110 (2 个计算置位,2 是质数)
2026
7 -&gt; 111 (3 个计算置位,3 是质数)
2127
9 -&gt; 1001 (2 个计算置位,2 是质数)
2228
10-&gt; 1010 (2 个计算置位,2 是质数)
29+
共计 4 个计算置位为质数的数字。
2330
</pre>
2431

25-
<p><strong>示例 2:</strong></p>
32+
<p><strong>示例 2</strong></p>
2633

2734
<pre>
28-
<strong>输入:</strong> L = 10, R = 15
29-
<strong>输出:</strong> 5
30-
<strong>解释:</strong>
35+
<strong>输入</strong>left = 10, right = 15
36+
<strong>输出</strong>5
37+
<strong>解释</strong>
3138
10 -&gt; 1010 (2 个计算置位, 2 是质数)
3239
11 -&gt; 1011 (3 个计算置位, 3 是质数)
3340
12 -&gt; 1100 (2 个计算置位, 2 是质数)
3441
13 -&gt; 1101 (3 个计算置位, 3 是质数)
3542
14 -&gt; 1110 (3 个计算置位, 3 是质数)
3643
15 -&gt; 1111 (4 个计算置位, 4 不是质数)
44+
共计 5 个计算置位为质数的数字。
3745
</pre>
3846

39-
<p><strong>注意:</strong></p>
47+
<p>&nbsp;</p>
48+
49+
<p><strong>提示:</strong></p>
4050

41-
<ol>
42-
<li><code>L, R</code>&nbsp;是&nbsp;<code>L &lt;= R</code>&nbsp;且在&nbsp;<code>[1, 10^6]</code>&nbsp;中的整数。</li>
43-
<li><code>R - L</code>&nbsp;的最大值为 10000。</li>
44-
</ol>
51+
<ul>
52+
<li><code>1 &lt;= left &lt;= right &lt;= 10<sup>6</sup></code></li>
53+
<li><code>0 &lt;= right - left &lt;= 10<sup>4</sup></code></li>
54+
</ul>
4555

4656
## 解法
4757

4858
<!-- 这里可写通用的实现逻辑 -->
4959

60+
题目中 `left``right` 的范围均在 10<sup>6</sup> 内,而 2<sup>20</sup>=1048576,因此二进制中 1 的个数最多也就 20 个,20 以内的质数为 {2, 3, 5, 7, 11, 13, 17, 19}。
61+
62+
我们可以遍历 `[left, right]` 范围内的每个数,计算出每个数的二进制表示中 1 的个数,判断此个数是否在上述列举的质数中,是则累加结果。
63+
64+
时间复杂度 `O((right-left)*log right)`,其中求二进制中 1 的个数的时间为 `O(log right)`
65+
5066
<!-- tabs:start -->
5167

5268
### **Python3**
5369

5470
<!-- 这里可写当前语言的特殊实现逻辑 -->
5571

5672
```python
57-
73+
class Solution:
74+
def countPrimeSetBits(self, left: int, right: int) -> int:
75+
primes = {2, 3, 5, 7, 11, 13, 17, 19}
76+
return sum(i.bit_count() in primes for i in range(left, right + 1))
5877
```
5978

6079
### **Java**
6180

6281
<!-- 这里可写当前语言的特殊实现逻辑 -->
6382

6483
```java
84+
class Solution {
85+
private static Set<Integer> primes = new HashSet<>(Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19));
86+
87+
public int countPrimeSetBits(int left, int right) {
88+
int ans = 0;
89+
for (int i = left; i <= right; ++i) {
90+
if (primes.contains(Integer.bitCount(i))) {
91+
++ans;
92+
}
93+
}
94+
return ans;
95+
}
96+
}
97+
```
98+
99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
unordered_set<int> primes{2, 3, 5, 7, 11, 13, 17, 19};
105+
106+
int countPrimeSetBits(int left, int right) {
107+
int ans = 0;
108+
for (int i = left; i <= right; ++i)
109+
if (primes.count(__builtin_popcount(i)))
110+
++ans;
111+
return ans;
112+
}
113+
};
114+
```
65115

116+
### **Go**
117+
118+
```go
119+
func countPrimeSetBits(left int, right int) int {
120+
primes := map[int]bool{2: true, 3: true, 5: true, 7: true, 11: true, 13: true, 17: true, 19: true}
121+
ans := 0
122+
for i := left; i <= right; i++ {
123+
if primes[bits.OnesCount(uint(i))] {
124+
ans++
125+
}
126+
}
127+
return ans
128+
}
66129
```
67130

68131
### **...**

solution/0700-0799/0762.Prime Number of Set Bits in Binary Representation/README_EN.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,60 @@
5757
### **Python3**
5858

5959
```python
60-
60+
class Solution:
61+
def countPrimeSetBits(self, left: int, right: int) -> int:
62+
primes = {2, 3, 5, 7, 11, 13, 17, 19}
63+
return sum(i.bit_count() in primes for i in range(left, right + 1))
6164
```
6265

6366
### **Java**
6467

6568
```java
69+
class Solution {
70+
private static Set<Integer> primes = new HashSet<>(Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19));
71+
72+
public int countPrimeSetBits(int left, int right) {
73+
int ans = 0;
74+
for (int i = left; i <= right; ++i) {
75+
if (primes.contains(Integer.bitCount(i))) {
76+
++ans;
77+
}
78+
}
79+
return ans;
80+
}
81+
}
82+
```
83+
84+
### **C++**
85+
86+
```cpp
87+
class Solution {
88+
public:
89+
unordered_set<int> primes{2, 3, 5, 7, 11, 13, 17, 19};
90+
91+
int countPrimeSetBits(int left, int right) {
92+
int ans = 0;
93+
for (int i = left; i <= right; ++i)
94+
if (primes.count(__builtin_popcount(i)))
95+
++ans;
96+
return ans;
97+
}
98+
};
99+
```
66100

101+
### **Go**
102+
103+
```go
104+
func countPrimeSetBits(left int, right int) int {
105+
primes := map[int]bool{2: true, 3: true, 5: true, 7: true, 11: true, 13: true, 17: true, 19: true}
106+
ans := 0
107+
for i := left; i <= right; i++ {
108+
if primes[bits.OnesCount(uint(i))] {
109+
ans++
110+
}
111+
}
112+
return ans
113+
}
67114
```
68115

69116
### **...**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
unordered_set<int> primes{2, 3, 5, 7, 11, 13, 17, 19};
4+
5+
int countPrimeSetBits(int left, int right) {
6+
int ans = 0;
7+
for (int i = left; i <= right; ++i)
8+
if (primes.count(__builtin_popcount(i)))
9+
++ans;
10+
return ans;
11+
}
12+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func countPrimeSetBits(left int, right int) int {
2+
primes := map[int]bool{2: true, 3: true, 5: true, 7: true, 11: true, 13: true, 17: true, 19: true}
3+
ans := 0
4+
for i := left; i <= right; i++ {
5+
if primes[bits.OnesCount(uint(i))] {
6+
ans++
7+
}
8+
}
9+
return ans
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
private static Set<Integer> primes = new HashSet<>(Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19));
3+
4+
public int countPrimeSetBits(int left, int right) {
5+
int ans = 0;
6+
for (int i = left; i <= right; ++i) {
7+
if (primes.contains(Integer.bitCount(i))) {
8+
++ans;
9+
}
10+
}
11+
return ans;
12+
}
13+
}
Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
class Solution:
2-
def countPrimeSetBits(self, L, R):
3-
"""
4-
:type L: int
5-
:type R: int
6-
:rtype: int
7-
"""
8-
res = 0
9-
flag = [2, 3, 5, 7, 11, 13, 17, 19]
10-
for i in range(L, R + 1):
11-
if bin(i).count('1') in flag:
12-
res += 1
13-
return res
1+
class Solution:
2+
def countPrimeSetBits(self, left: int, right: int) -> int:
3+
primes = {2, 3, 5, 7, 11, 13, 17, 19}
4+
return sum(i.bit_count() in primes for i in range(left, right + 1))

solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
```python
7373
class Solution:
7474
def sortByBits(self, arr: List[int]) -> List[int]:
75-
arr.sort(key=lambda x: (bin(x).count("1"), x))
75+
arr.sort(key=lambda x : (x.bit_count(), x))
7676
return arr
7777
```
7878

solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ The sorted array by bits is [0,1,2,4,8,3,5,6,7]
4646
```python
4747
class Solution:
4848
def sortByBits(self, arr: List[int]) -> List[int]:
49-
arr.sort(key=lambda x: (bin(x).count("1"), x))
49+
arr.sort(key=lambda x : (x.bit_count(), x))
5050
return arr
5151
```
5252

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class Solution:
2-
def sortByBits(self, arr: List[int]) -> List[int]:
3-
arr.sort(key=lambda x: (bin(x).count("1"), x))
4-
return arr
1+
class Solution:
2+
def sortByBits(self, arr: List[int]) -> List[int]:
3+
arr.sort(key=lambda x: (x.bit_count(), x))
4+
return arr

0 commit comments

Comments
 (0)