204. Count Primes
# Easy
class Solution {
public:
int countPrimes(int n) {
if(n <= 2) return 0;
int count = 0;
vector<bool> isPrime(n, true);
for(int i = 2; i < n; i ++) {
if(!isPrime[i]){
continue;
}
count ++;
for(int j = 2; i*j < n; j ++) {
isPrime[i*j] = false;
}
}
return count;
}
};
Hightlight initilize an array with length of a variable:
vector<bool> isPrime(n, true);
// wrong expression
// bool isPrime[n];
Last updated
Was this helpful?