Count Primes

题目描述:

Count the number of prime numbers less than a non-negative number, n.

解题思路:使用素数筛选法

class Solution {
public:
    int countPrimes(int n) {
      vector<bool> prime(n, true);
		prime[0] = false, prime[1] = false;
		for (int i = 0; i < sqrt(n); ++i) {
			if (prime[i]) {
				for (int j = i*i; j < n; j += i) {
					prime[j] = false;
				}
			}
		}
		return count(prime.begin(), prime.end(), true);
    }
};


你可能感兴趣的:(LeetCode,C++,count,primes)