-
Notifications
You must be signed in to change notification settings - Fork 3
/
_0204_CountPrimes.java
55 lines (52 loc) · 1.4 KB
/
_0204_CountPrimes.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
package com.diguage.algorithm.leetcode;
import java.util.HashSet;
import java.util.Set;
/**
* = 204. Count Primes
*
* https://leetcode.com/problems/count-primes/[Count Primes - LeetCode]
*
* ount the number of prime numbers less than a non-negative number, **n**.
*
* .Example:
* [source]
* ----
* Input: 10
* Output: 4
* Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
* ----
*
* @author D瓜哥, https://www.diguage.com/
* @since 2020-01-10 23:04
*/
public class _0204_CountPrimes {
/**
* Runtime: 163 ms, faster than 25.96% of Java online submissions for Count Primes.
*
* Memory Usage: 93.7 MB, less than 5.66% of Java online submissions for Count Primes.
*/
public int countPrimes(int n) {
if (n < 3) {
return 0;
}
Set<Integer> numbers = new HashSet<>();
numbers.add(2);
for (int i = 3; i < n; ) {
numbers.add(i);
i += 2;
}
for (int i = 3; i < Math.sqrt(n); ) {
for (int j = 3; i * j < n; ) {
numbers.remove(i * j);
j += 2;
}
i += 2;
}
return numbers.size();
}
public static void main(String[] args) {
_0204_CountPrimes solution = new _0204_CountPrimes();
int r1 = solution.countPrimes(100);
System.out.println((r1 == 4) + " : " + r1);
}
}