leetcode 204. Count Primes
2016-10-19 16:22
381 查看
/* **leetcode 204. Count Primes** Description: Count the number of prime numbers less than a non-negative number, n. 题目大意:求小于n个素数的个数 解题思路:打表,素数筛选法求素数,然后统计。 */ #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; class Solution { public: int countPrimes(int n) { //打表 vector<int> isPrime(n, true); for (int i = 2; i * i <= n; ++i) { if (isPrime[i]) { //2...i,把这些数的倍数不是素数 for (int j = i *i; j < n; j += i) isPrime[j] = false; } } int ret = 0; for (int i = 2; i < n; ++i) if (isPrime[i]) ++ret; return ret; } }; void test() { Solution sol; cout << sol.countPrimes(10) << endl; } int main() { test(); return 0; }
相关文章推荐
- 【leetcode】204. Count Primes
- LeetCode - 204. Count Primes
- Leetcode 204. Count Primes
- LeetCode:204. Count Primes
- (LeetCode) 204. Count Primes
- LeetCode-Hash-204. Count Primes
- Leetcode:204. Count Primes 求素数的优化问题
- leetcode - 204. Count Primes
- [leetcode]204. Count Primes
- leetcode 204. Count Primes
- [Leetcode]204. Count Primes
- LeetCode 204. Count Primes
- [LeetCode]204. Count Primes
- LeetCode 204. Count Primes
- Leetcode 204. Count Primes
- [leetcode] 204. Count Primes
- leetcode(52).204. Count Primes
- leetcode-204. Count Primes
- LeetCode笔记:204. Count Primes
- 【LeetCode】 204. Count Primes