标签:
Problem:
Description:
Count the number of prime numbers less than a non-negative number, n.
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
Solution:采用的为埃拉托斯特尼筛法
算法描述:(来自百度百科)
class Solution { public: int countPrimes(int n) { vector<bool> flags(n-1,true); flags[0]=false; int res; int limit=sqrt(n); for(int i=2;i<=limit;i++) { if(flags[i-1]) { for(int j=i*i;j<n;j+=i) { flags[j-1]=false; } } } for (int j = 0; j < n-1; ++j) { if (flags[j]) ++res; } return res; } };
标签:
原文地址:http://www.cnblogs.com/xiaoying1245970347/p/4581896.html