Count Primes
题目描述:
Description:
Count the number of prime numbers less than a non-negative number, n.
题目思路:
题目给出一个数字n,求出小于n的素数的个数。
打表晒素数,统计素数的个数返回即可。
题目代码:
class Solution {
public:
int countPrimes(int n) {
vector<int> check(n,1);
int cnt = 0;
for(int i = 2; i*i < n; i++){
if(check[i]){
for(int j = i*i; j < n; j+=i){
check[j] = 0;
}
}
}
for(int i = 2; i < n; i++){
if(check[i])
cnt++;
}
return cnt;
}
};