forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0204.cpp
More file actions
31 lines (28 loc) · 697 Bytes
/
0204.cpp
File metadata and controls
31 lines (28 loc) · 697 Bytes
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
#include <deque>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
int countPrimes(int n)
{
if (n <= 2) return 0;
int m = n/2, count = m, u = sqrt(n)/2;
deque<bool> flag(m, 0);
for (int i = 1; i <= u; i++)
{
if (!flag[i])
{
for (int k = (i+ 1)*2*i; k < m; k += i*2 + 1)
{
if (!flag[k])
{
flag[k] = true;
count--;
}
}
}
}
return count;
}
};