204. Count Primes

2018/04/12 leetcode 共 359 字,约 2 分钟

Title

the problem link

Description:

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

题目大意:计算小于n的素数个数

Solution

//cpp:
class Solution {
public:
	int countPrimes(int n) {
		int count = 0;
		vector<bool> v(n);
		fill(v.begin(), v.end(), false);
		for (int i = 2; i < n; i++) {
			if (v[i])
				continue;
			count++;
			for (int j = i+i; j < n; j += i) {
				v[j] = true;
			}
		}
		return count;
	}
};

Search

    Table of Contents