LeetCode之Count Primes

来源:互联网 发布:软件项目预算模板 编辑:程序博客网 时间:2024/04/27 18:36
/*根据提示求解。*/class Solution {public:    int countPrimes(int n) {        bool mark[n];        memset(mark, 1, sizeof(mark));        int sqrt_n = sqrt(n);        for(int i = 2; i <= sqrt_n; ++i){            if(mark[i]){                for(int j = i*i; j < n; j += i){                mark[j] = false;              }            }        }        int count = 0;         for(int i = 2; i < n; ++i){            if(mark[i]) ++count;        }        return count;    }};

0 0
原创粉丝点击