码迷,mamicode.com
首页 > 其他好文 > 详细

204. Count Primes(LeetCode)

时间:2017-07-17 11:10:50      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:tor   res   color   des   turn   solution   pre   class   number   

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)

标签:tor   res   color   des   turn   solution   pre   class   number   

原文地址:http://www.cnblogs.com/wujufengyun/p/7193077.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!