AM 1324. Count Primes
class Solution {
public:
/**
* @param n: a integer
* @return: return a integer
*/
int countPrimes(int n) {
// write your code here
if(n <= 2){
return 0;
}
std::vector<bool> notPrime(n - 1, false);
int res = 0;
for(int i = 2; i < n; i++){
if(notPrime[i] == false){
res++;
}
for(int j = 2; i * j < n; j++){
notPrime[i * j] = true;
}
}
return res;
}
};
Comments
Post a Comment