Count Primes
https://leetcode.com/problems/count-primes/description/
Thoughts
Code
/*
* @lc app=leetcode id=204 lang=cpp
*
* [204] Count Primes
*/
// @lc code=start
class Solution {
public:
int countPrimes(int n) {
vector<bool> prims(n, true);
for (int i = 2; i < sqrt(n); ++i) {
if (!prims[i]) continue;
for (int j = 2; i * j < n; ++j) {
prims[i * j] = false;
}
}
int res = 0;
for (int i = 2; i < n; ++i) {
if (prims[i]) {
++res;
}
}
return res;
}
};
// @lc code=end
Analysis
Last updated