204. Count Primes(LeetCode)

Posted 无惧风云

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了204. Count Primes(LeetCode)相关的知识,希望对你有一定的参考价值。

Description:

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

 1 class Solution {
 2 public:
 3     int countPrimes(int n) {
 4       vector<bool> num(n - 1, true);
 5         num[0] = false;
 6         int res = 0, limit = sqrt(n);
 7         for (int i = 2; i <= limit; ++i) {
 8             if (num[i - 1]) {
 9                 for (int j = i * i; j < n; j += i) {
10                     num[j - 1] = false;
11                 }
12             }
13         }
14         for (int j = 0; j < n - 1; ++j) {
15             if (num[j]) ++res;
16         }
17         return res;
18     }
19 };

 

以上是关于204. Count Primes(LeetCode)的主要内容,如果未能解决你的问题,请参考以下文章

204. Count Primes

#Leetcode# 204. Count Primes

204. Count Primes

204. Count Primes

204. Count Primes

204. Count Primes