Skip to content

Commit 52de171

Browse files
committed
feat: add solutions to leetcode problem: No.0204
See https://leetcode-cn.com/problems/count-primes
1 parent f54c7c1 commit 52de171

File tree

6 files changed

+98
-26
lines changed

6 files changed

+98
-26
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
1. [数组中数字出现的次数 II](/lcof/面试题56%20-%20II.%20数组中数字出现的次数%20II/README.md)
101101
1. [错误的集合](/solution/0600-0699/0645.Set%20Mismatch/README.md)
102102
1. [二进制中 1 的个数](/lcof/面试题15.%20二进制中1的个数/README.md)
103+
1. [计数质数](/solution/0200-0299/0204.Count%20Primes/README.md)
103104

104105
### 栈和队列
105106

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
9595
### Math
9696

9797
1. [Set Mismatch](/solution/0600-0699/0645.Set%20Mismatch/README_EN.md)
98+
1. [Count Primes](/solution/0200-0299/0204.Count%20Primes/README_EN.md)
9899

99100
### Stack & Queue
100101

solution/0200-0299/0204.Count Primes/README.md

+37-2
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,57 @@
1818

1919
<!-- 这里可写通用的实现逻辑 -->
2020

21+
如果 x 是质数,那么大于 x 的 x 的倍数 2x,3x,… 一定不是质数,因此我们可以从这里入手。
22+
23+
我们设 `primes[i]` 表示数 i 是不是质数,如果是质数则为 true,否则为 false。从小到大遍历每个数,如果这个数为质数,则将其所有的倍数都标记为合数(除了该质数本身),即 false,这样在运行结束的时候我们即能知道质数的个数。
24+
25+
对于一个质数 x,我们从 2x 开始标记其实是冗余的,应该直接从 x⋅x 开始标记,因为 2x,3x,… 这些数一定在 x 之前就被其他数的倍数标记过了,例如 2 的所有倍数,3 的所有倍数等。
26+
2127
<!-- tabs:start -->
2228

2329
### **Python3**
2430

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

2733
```python
28-
34+
class Solution:
35+
def countPrimes(self, n: int) -> int:
36+
if n < 2:
37+
return 0
38+
res = 0
39+
primes = [True for _ in range(n)]
40+
for i in range(2, n):
41+
if primes[i]:
42+
res += 1
43+
for j in range(i * i, n, i):
44+
primes[j] = False
45+
return res
2946
```
3047

3148
### **Java**
3249

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

3552
```java
36-
53+
class Solution {
54+
public int countPrimes(int n) {
55+
if (n < 2) return 0;
56+
boolean[] primes = new boolean[n];
57+
Arrays.fill(primes, true);
58+
int res = 0;
59+
for (int i = 2; i < n; ++i) {
60+
if (primes[i]) {
61+
++res;
62+
if ((long) i * i < n) {
63+
for (int j = i * i; j < n; j += i) {
64+
primes[j] = false;
65+
}
66+
}
67+
}
68+
}
69+
return res;
70+
}
71+
}
3772
```
3873

3974
### **...**

solution/0200-0299/0204.Count Primes/README_EN.md

+31-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,42 @@
2525
### **Python3**
2626

2727
```python
28-
28+
class Solution:
29+
def countPrimes(self, n: int) -> int:
30+
if n < 2:
31+
return 0
32+
res = 0
33+
primes = [True for _ in range(n)]
34+
for i in range(2, n):
35+
if primes[i]:
36+
res += 1
37+
for j in range(i * i, n, i):
38+
primes[j] = False
39+
return res
2940
```
3041

3142
### **Java**
3243

3344
```java
34-
45+
class Solution {
46+
public int countPrimes(int n) {
47+
if (n < 2) return 0;
48+
boolean[] primes = new boolean[n];
49+
Arrays.fill(primes, true);
50+
int res = 0;
51+
for (int i = 2; i < n; ++i) {
52+
if (primes[i]) {
53+
++res;
54+
if ((long) i * i < n) {
55+
for (int j = i * i; j < n; j += i) {
56+
primes[j] = false;
57+
}
58+
}
59+
}
60+
}
61+
return res;
62+
}
63+
}
3564
```
3665

3766
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
1-
public class Solution {
1+
class Solution {
22
public int countPrimes(int n) {
3-
4-
boolean[] b = new boolean[n];
5-
6-
for (int i = 2; i * i < n; i++) {
7-
if (b[i] == false) {
8-
for (int j = i; i * j < n; j++) {
9-
b[i * j] = true;
10-
}
11-
}
12-
}
13-
14-
int c = 0;
15-
for (int i = 2; i < n; i++) {
16-
if (b[i] == false) {
17-
c++;
18-
}
19-
}
20-
21-
return c;
22-
23-
3+
if (n < 2) return 0;
4+
boolean[] primes = new boolean[n];
5+
Arrays.fill(primes, true);
6+
int res = 0;
7+
for (int i = 2; i < n; ++i) {
8+
if (primes[i]) {
9+
++res;
10+
if ((long) i * i < n) {
11+
for (int j = i * i; j < n; j += i) {
12+
primes[j] = false;
13+
}
14+
}
15+
}
16+
}
17+
return res;
2418
}
2519
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def countPrimes(self, n: int) -> int:
3+
if n < 2:
4+
return 0
5+
res = 0
6+
primes = [True for _ in range(n)]
7+
for i in range(2, n):
8+
if primes[i]:
9+
res += 1
10+
for j in range(i * i, n, i):
11+
primes[j] = False
12+
return res

0 commit comments

Comments
 (0)