-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo204CountPrimes.java
60 lines (51 loc) · 1.2 KB
/
No204CountPrimes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.wzx.leetcode;
import java.util.BitSet;
/**
* @author wzx
* @see <a href="https://leetcode.com/problems/count-primes/">https://leetcode.com/problems/count-primes/</a>
*/
public class No204CountPrimes {
/**
* 暴力搜索, 超时
* <p>
* time: O(n*sqrt(n))
* space: O(1)
*/
public int countPrimes1(int n) {
int count = 0;
for (int i = 2; i < n; i++) {
if (isPrime(i)) count++;
}
return count;
}
private boolean isPrime(int n) {
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
/**
* Sieve of Eratosthenes
* 用位图保存是否素数信息
* <p>
* time: O(n*loglogn)
* space: O(n)
*/
public int countPrimes2(int n) {
if (n < 2) return 0;
BitSet primes = new BitSet(n);
primes.set(2, n);
for (int i = 2; i <= Math.sqrt(n); i++) {
if (primes.get(i)) {
// cur*2, cur*3, ..., cur*n 不是素数
// 由于是由前向后遍历, cur*2, cur*3, ..., cur*cur 已经被 2,3,...cur标记过了
for (int j = i * i; j <= n; j += i) {
primes.set(j, false);
}
}
}
return primes.cardinality();
}
}