-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashtable_LC204.cpp
71 lines (65 loc) · 1.22 KB
/
hashtable_LC204.cpp
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
61
62
63
64
65
66
67
68
69
70
71
#include<iostream>
using namespace std;
#include<vector>
#include<math.h>
/*
time limited exceeded
change to sqrt(i):
get accepted
*/
class Solution {
public:
int countPrimes(int n) {
if (n <= 2) {
return 0;
}
vector<int> primes;
primes.push_back(2);
int count = 1;
for (int i = 3; i < n; i++) {
for (int j = 0; j < primes.size(); j++) {
if (i%primes[j] != 0&& (j == primes.size() - 1||primes[j]>sqrt(i))) {
primes.push_back(i);
count++;
break;
}
if (i%primes[j] != 0) {
continue;
}
else {
break;
}
}
}
return count;
}
};
/*
The Sieve of Eratosthenes is one of the most efficient ways to find all prime numbers up to n.
*/
class Solution {
public:
int countPrimes(int n) {
vector<bool> isPrime;
/*
why I use while here does not work
while (n-- > 0) {
isPrime.push_back(true);
}
*/
for (int i = 0; i < n; i++) {
isPrime.push_back(true);
}
for (int i = 2; i*i < n; i++) {
if (!isPrime[i])continue;
for (int j = i * i; j < n; j+=i) {
isPrime[j] = false;
}
}
int count = 0;
for (int i = 2; i < n; i++) {
if (isPrime[i]) count++;
}
return count;
}
};