Skip to content

Commit 8e39008

Browse files
Add files via upload
1 parent 2fc2ded commit 8e39008

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Count Primes/Count_Primes.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// 附参考链接
2+
https://leetcode.com/problems/count-primes/discuss/?currentPage=1&orderBy=most_votes&query=
3+
https://assets.leetcode.com/static_assets/public/images/solutions/Sieve_of_Eratosthenes_animation.gif
4+
5+
6+
class Solution
7+
{
8+
public:
9+
int countPrimes(int n)
10+
{
11+
// 边界条件处理
12+
if (n < 2) return 0;
13+
14+
vector<bool> memo(n, true);
15+
memo[0] = false;
16+
memo[1] = false;
17+
18+
for (int i = 2; i <= sqrt(n); ++i)
19+
if (memo[i])
20+
for (int j = 2; i * j < n; ++j)
21+
memo[i * j] = false;
22+
23+
int res = 0;
24+
for (auto item : memo)
25+
if (item)
26+
++res;
27+
return res;
28+
}
29+
};

0 commit comments

Comments
 (0)