Description:
Count the number of prime numbers less than a non-negative number, n.
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
class Solution {
public:
int countPrimes(int n) {
if(n < 2) return 0;
vector<int> visited(n, 0);
int cnt = 0;
for(int i=2; i<n; i++) {
if(visited[i]==1) continue;
cnt += 1;
for(int j=i+i; j < n; j+=i)
visited[j] = 1;
}
return cnt;
}
};